 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what. v$ i9 m% x% |4 W3 K" I; K. N
基本参数:
! t& z7 P9 x( e& D: r, Fjta | thread | managed | custom.Class
8 u C/ N/ ]8 {( t" e1 F" y6 T主要是配合! n2 B8 j7 @9 ?2 D
SessionFactory.getCurrentSession(). u9 k) X4 x+ M' b8 m3 m/ f
来用的.
! K5 X' z. `- z6 H. JThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,9 S0 [! m* S8 h, s! i6 t& ^& \
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
. C8 k$ \. e1 O6 J# w. d5 q1 wcontext.BeginRequest += OpenSession;
) Y! }5 [0 g! p5 a" h. o$ {.Bind(HttpContext.Current, SessionFactory.OpenSession())9 t" a; }( L! Y. j i' z [
; [4 ^/ p, V# _4 V0 J& `+ z
//do transaction
|3 |* x5 i/ E' _3 J# s5 ]+ y8 w- VgetCurrentSession().Submit();
) i( S$ C" V& g* {; V7 y" F: tgetCurrentSession().RollBack();& U f+ s( G- s+ Z& o1 N1 O
...
/ W0 b [8 E( Y$ g8 A9 h7 D1 DUnbind(HttpContext.Current, SessionFactory)
, x% \* s2 p' s+ r: |0 [8 G4 C. I5 Q k; L9 c6 K1 t
context.EndRequest += CloseSession;1 ?! v/ T" c3 Y
@0 R1 g0 e+ O7 n4 Y 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|