 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
1 g# Z2 d' G" z, J( Y: h' b. ?基本参数:0 l7 Z/ O* {) z4 I
jta | thread | managed | custom.Class, N, |+ c" X& U% p# \6 n
主要是配合, ?" S5 @% \/ z3 \4 B# X. r: _
SessionFactory.getCurrentSession()
; @8 @5 x( P ?9 w% f6 i3 C: O2 A来用的.' \+ n5 B0 O1 l
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,9 f' x, v" V9 U0 y9 ]- [ Y
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:% m& m$ L: |5 G* N, n$ B$ f% c `, \8 l- l
context.BeginRequest += OpenSession;
) f9 @( Y/ S+ Z' ?& f9 x1 J.Bind(HttpContext.Current, SessionFactory.OpenSession()). Z- D) F: g- t0 k7 h8 [& ~
: W3 }- ~: n3 u
//do transaction
/ Q0 T* N4 j% \) OgetCurrentSession().Submit();6 L4 x; e4 Z) V! u. W$ d4 o0 y* `
getCurrentSession().RollBack();$ C; g+ t- q0 y+ |2 F9 W
...
@9 f1 D# `3 BUnbind(HttpContext.Current, SessionFactory)
8 c" }9 i3 S* A7 O9 K
! Z' Z* ?3 F2 O3 o7 @context.EndRequest += CloseSession;: t& p$ }! |8 @2 i+ w; d1 `
+ q4 Y. h$ T' O3 Q+ J 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|