 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
6 k; \8 y- |: z, e. t4 X! l, C基本参数:. u# g2 e- K, ~) C0 g
jta | thread | managed | custom.Class
y+ R$ K+ y0 N7 u4 d, ?1 z$ p4 L主要是配合
3 A) Z, u8 f0 z" }) L1 I! ~& gSessionFactory.getCurrentSession()
# J6 i; N) K" P+ t9 K- C9 z @, N来用的.
3 A; P1 k+ H5 ?+ C( I6 E7 |Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
5 ]' J% c+ C! j/ {- K, Q* U7 c% ~- @Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
; Z% V- c0 C4 {8 Dcontext.BeginRequest += OpenSession;
! g5 ~" W/ g. q, P$ C.Bind(HttpContext.Current, SessionFactory.OpenSession())0 |$ q: X5 Q* V
3 F0 m j" o! }9 m8 _) D
//do transaction
+ H" z: y7 U9 g3 E+ F( @6 IgetCurrentSession().Submit();. J3 ^$ Q8 b$ h7 g- F, E2 R
getCurrentSession().RollBack();! C) F- W# f6 T
...
4 K2 ^, m& W* p" ]3 M4 aUnbind(HttpContext.Current, SessionFactory). h9 C! ]) B; y. q- `
1 N1 Y- u% {3 Zcontext.EndRequest += CloseSession;
( V) {) d" O2 B5 S( }5 S! D+ ~2 s" k
" n: p" [& ]1 C+ G" _ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|