 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
2 k% J* K# T, q, v1 d, @+ a; t( B基本参数:
( v, T& D' ?( D1 \2 hjta | thread | managed | custom.Class$ H3 O: C/ q- b
主要是配合 }3 x7 \) ~. d; b3 ^
SessionFactory.getCurrentSession()# n' S8 E/ R' X9 a+ Q( v' t
来用的.4 {1 S$ t- O! I& V) g2 s
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
, s7 t t: z, V/ U5 x3 N) Q9 dWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
& c# y+ J2 i/ ycontext.BeginRequest += OpenSession;
. ] y& U- e" [.Bind(HttpContext.Current, SessionFactory.OpenSession()) l- G, I2 p' v7 x7 n1 s
+ Y$ @2 u" s8 ?) i6 r
//do transaction
' R3 h' i2 N- ]5 C' {getCurrentSession().Submit();
" S( A. r5 O" k: }/ I W0 jgetCurrentSession().RollBack();
. Z2 C$ o* c, s9 q0 {# G...0 V; H2 B, G8 u9 p# X1 P4 Q2 P
Unbind(HttpContext.Current, SessionFactory)
6 U# m0 T3 `1 [
Z+ } ?5 v$ c) Tcontext.EndRequest += CloseSession;
0 r0 y4 d8 `7 i7 a4 l0 W; M
% C: V5 b& U3 j 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|