 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
" u8 G( G$ [$ E9 R基本参数:
6 p! U+ a" l' d7 y9 Ljta | thread | managed | custom.Class
( r* t! C. ~9 j9 Z' [& Y主要是配合- Z+ l Y+ J _5 ]* @
SessionFactory.getCurrentSession()# p: r/ o+ h& v) j! }
来用的.
+ ~/ @9 E9 ?# M* ]Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,$ a# w: {; Q! K- c
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
: I1 a5 Y4 f2 t8 m/ j, w/ |2 H) ~! fcontext.BeginRequest += OpenSession;
' J1 K1 ~. ?+ S5 k- k3 B C G.Bind(HttpContext.Current, SessionFactory.OpenSession())) u! M' Y& J( K
: e3 M6 {, j/ m3 s
//do transaction z+ u: o/ }6 R; ~! v7 ]
getCurrentSession().Submit();
: L2 x% l& b9 g/ F/ z. a/ C, wgetCurrentSession().RollBack();/ H3 {! M; |/ `/ c4 y$ `
...
8 J, Z ]' F8 Y9 R9 U9 fUnbind(HttpContext.Current, SessionFactory)0 y6 h& k0 T: a v& P# M
# v& t7 o; T Y. bcontext.EndRequest += CloseSession;
1 k9 D- L) Q0 |" W# b- b
; l' a. ?# N2 W2 ] 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|