 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what q j& m: g2 X- ~
基本参数:
$ E9 o: }6 t7 f4 J% H6 P, \jta | thread | managed | custom.Class8 G5 }$ i: M6 Q0 h' |3 E
主要是配合* }, S2 A' T& B4 t4 y o. d5 p) Y
SessionFactory.getCurrentSession()
% z- I: t C7 |/ B来用的.
& ~: u* Z+ O" u' x4 r, N4 mThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,$ L- u6 y J- q1 v4 c/ y
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
* T5 ^; `: N2 I2 Econtext.BeginRequest += OpenSession;
; e/ n; f+ |3 {, @.Bind(HttpContext.Current, SessionFactory.OpenSession())
0 C& t e: F0 O, V# |: o4 h. h. u6 I" M" B6 L
//do transaction( s F0 C, c9 G* w, L3 g
getCurrentSession().Submit();- f$ f g+ H/ R, [' }
getCurrentSession().RollBack();" I+ C9 b& @/ _, u) X) v
...4 l+ I8 P# C/ J! f: y
Unbind(HttpContext.Current, SessionFactory)
6 N4 G8 v( e c& Y, x9 O, d( P7 `2 p' q
context.EndRequest += CloseSession;
- V. }5 p6 @4 U& Z' y1 l, X0 Q0 H6 q2 p* j1 H+ I
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|