 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
* U- O) w5 ~" E& c( i基本参数:" O7 D0 n6 @- V. Y# @5 A
jta | thread | managed | custom.Class
' e$ R2 y ^6 M- Y+ N" m0 w主要是配合2 V7 `2 Z8 b2 q, e& H8 Y; ?3 ]7 s
SessionFactory.getCurrentSession()/ ~# ]6 m- }7 j. F. d- }
来用的.5 j" L! ], G: ], z3 k- M {8 s
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,' L3 j9 L% y" Z& I" V p/ Q+ y3 I
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:( e7 _6 {+ z* Y5 s1 H }
context.BeginRequest += OpenSession;
" H2 E6 O& x% A9 R a$ C.Bind(HttpContext.Current, SessionFactory.OpenSession())
( J% y U* B- [$ [0 x' u. N3 X3 c4 P F; c# h, f; F4 W, {( P
//do transaction* L8 Y% e a8 _! [3 K: N6 N5 E8 s
getCurrentSession().Submit();* E9 A6 [: ]5 {: R
getCurrentSession().RollBack();( T6 n4 E m, f9 C r
...
- @4 v: ~" a6 Y' @: ZUnbind(HttpContext.Current, SessionFactory)! p N+ n4 ?2 `
& U. R' b% u. v* R5 D- G Lcontext.EndRequest += CloseSession;
/ E$ K" E' O. S' }3 ^) A% ]4 I) g v3 j* j
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|