 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
7 o8 c ]& i2 }2 T; X; x( e基本参数:$ c e& f9 o7 @( E& G
jta | thread | managed | custom.Class
! O- U1 M7 T0 h) E$ C主要是配合2 O* p, m; m) Z* n' A h0 f' ~3 f' g
SessionFactory.getCurrentSession(): r, X! }1 a# Z5 y0 v* F
来用的.
/ G/ U' l! D# J, b; LThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 \) P+ a1 j2 K e, I
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:& T1 I3 C' u8 x4 F" H, ?
context.BeginRequest += OpenSession;
% i$ R1 L7 D8 Y `3 z# y3 e+ `.Bind(HttpContext.Current, SessionFactory.OpenSession())
" ~* p4 d$ ]8 J/ P* F/ `: {+ V+ ~. E* ?( F! p
//do transaction) C% R$ M# }! ~ `2 V
getCurrentSession().Submit();. f- Z% O- A( x
getCurrentSession().RollBack();
0 X: @# [+ U3 Y' j6 x* O: ]5 d. X0 m...
" m2 h7 [1 |4 B& R* b0 Z1 fUnbind(HttpContext.Current, SessionFactory)
6 k7 G7 M! J6 u+ m0 h- Z" \9 F" N- {7 }; q5 V7 m
context.EndRequest += CloseSession;
: B7 U; r! b/ R0 o" G$ i0 q" Y: h2 _: S9 V! E6 \1 I2 d7 a
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|