 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
0 M# {2 {2 P2 m: g, Q2 H基本参数: K& F6 f3 @' G+ t0 q& e, R# c
jta | thread | managed | custom.Class
' ?5 x2 d i( X+ a主要是配合
X3 a7 k: l4 n+ _' \- B1 K$ Z3 ]SessionFactory.getCurrentSession()
* w/ W* {, i& f" f( b来用的.5 F" X$ C% |4 M2 [! u
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,1 J* L% ~7 _8 c/ e! ?9 S
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:) i- z% P. Q" p- G) ^4 W* X
context.BeginRequest += OpenSession;# A$ l& l2 y6 U9 `% r& r! K
.Bind(HttpContext.Current, SessionFactory.OpenSession())
2 ^6 J* \& \" K
/ n) k! F/ Y( d/ q. u: ~- a5 e//do transaction
3 ^5 v, \+ s# N5 Q9 FgetCurrentSession().Submit();4 D( f; P9 t, \
getCurrentSession().RollBack();) W8 d* x$ f* ]5 s/ a
..." |% E! D- s4 ^
Unbind(HttpContext.Current, SessionFactory), q# A5 e3 o4 A0 W I% X# N
# ]% W2 K/ r! |8 v& o5 Fcontext.EndRequest += CloseSession;
$ p2 A0 @* b0 e7 C! \+ A8 l4 C0 r4 R5 d& C
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|