 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what6 U. ]9 a9 b0 E8 H6 m
基本参数:+ F7 \7 ]. i8 I3 V2 y
jta | thread | managed | custom.Class
: c* i; c. p$ F; U5 g4 |+ S8 `主要是配合" U+ K+ i. h. }
SessionFactory.getCurrentSession()
& \ p$ d. S9 u. U- W" j q8 U来用的.
p& f4 w( q+ [5 x3 X- FThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
; v9 F* a' U3 ]3 C+ E) wWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
! y# v. |2 _( ^: h! }" C& C, Hcontext.BeginRequest += OpenSession;
& ]0 Q4 u$ }' C4 o5 N2 ^3 A.Bind(HttpContext.Current, SessionFactory.OpenSession())& k M& k& H2 ^8 ?3 F; d* D
3 R% ^3 p. M1 z5 O
//do transaction1 o( E! P2 I1 w
getCurrentSession().Submit();
7 [" d+ }& u# L2 _: o( igetCurrentSession().RollBack();
6 [$ O8 M7 U* u2 w...
% }3 f; V4 z3 C0 ]Unbind(HttpContext.Current, SessionFactory)
. n, R2 a5 F2 F! w) _/ x
9 s9 r- ]2 e; ?7 B! d6 r) C3 x$ \context.EndRequest += CloseSession;6 Z. ]0 ^# Q, E* G. y4 Q/ K& R) x$ B
2 G0 n( Q2 |: R' a* t+ ]( B
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|