 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what: n! n2 q1 J! x: _6 b
基本参数:" b6 K6 }0 o7 j3 c
jta | thread | managed | custom.Class
3 q4 b V9 o+ K! o# a0 f6 n主要是配合
' r9 o9 L, }. O- }1 C# C8 lSessionFactory.getCurrentSession()* X1 [" F1 a* Y8 L8 c
来用的.& Y3 D0 R. U0 F# H3 `/ E" J
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,: m5 D. `8 K0 |" c; P) ^
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:7 l! {9 B: W. L1 e1 }
context.BeginRequest += OpenSession;
& S. j9 ?: a+ X/ V! J& d( O5 V.Bind(HttpContext.Current, SessionFactory.OpenSession())
3 @) S. d5 v' p( ~4 n; o7 j
! X+ M2 k7 q/ g$ i3 x" h% O//do transaction
, @, u! J* g3 QgetCurrentSession().Submit();
7 S# M2 C5 a* n% M9 Y3 v7 jgetCurrentSession().RollBack();
" z" ^6 j5 M! K..." V4 _, g) \9 C. @% r
Unbind(HttpContext.Current, SessionFactory)$ T& T3 Z$ ], f5 e L9 G
' B$ R" ^2 Y2 m+ s9 J9 Gcontext.EndRequest += CloseSession;
8 u4 @) G1 \/ m5 M/ d7 t! ]0 Z5 x) d6 x/ o5 o1 j/ f' Y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|