 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
; w4 `5 F2 d3 L基本参数:
2 E( H5 } m* B% Njta | thread | managed | custom.Class
# M: t! ~* `6 P5 ^1 j主要是配合" l, H2 \) l# r; Q
SessionFactory.getCurrentSession()- Z8 P- k5 K; b" I9 L3 E6 _0 k
来用的.7 |/ L) t7 ^. y' L
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,! E- m c# h) c9 C& D
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
: H; O" I! Z. E4 t5 Mcontext.BeginRequest += OpenSession;
! D+ T& X$ M5 y4 b, e" x" h.Bind(HttpContext.Current, SessionFactory.OpenSession())
2 ^' j0 r0 ^. m( b- @# c) Y: C7 t+ M9 Y" h6 b b
//do transaction" L+ G. L D5 T3 l$ F
getCurrentSession().Submit();
2 f# j5 l, F7 q4 a( X9 mgetCurrentSession().RollBack();
) L4 H2 F' I! I: `9 W8 z& c...7 y# y+ ]# d) r1 h" j
Unbind(HttpContext.Current, SessionFactory)
' J' n/ w# z+ K" j& ]. I- K1 u0 {7 U
( w# H$ l$ n! N/ t4 K: U8 ucontext.EndRequest += CloseSession;0 {4 \4 C F( r3 ~; q1 N$ m* O
1 ?4 ~$ \( v% T1 N% M+ | 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|