 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
# r: U9 g8 d N* [# i* I基本参数:
3 a3 p0 t* R C& Xjta | thread | managed | custom.Class4 P- G/ B# {9 M7 S
主要是配合7 C3 z& x+ S, X3 @
SessionFactory.getCurrentSession()
) k# P9 a) E0 p% i2 U1 L来用的.
. o! j. f7 L! W- ]# S! G7 TThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,/ p/ h( v5 {3 m. I
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
: f- n- G4 v* h4 o3 Ccontext.BeginRequest += OpenSession;
% U! _9 e: V+ x9 `* o, s' G.Bind(HttpContext.Current, SessionFactory.OpenSession())
6 T1 s0 A( D) m- M, a" C; Z' h. \, X1 b, y
//do transaction
0 Y( B% t6 ^& r' n- |( P! ngetCurrentSession().Submit();( d- t: F& e2 B2 ]7 ~% r
getCurrentSession().RollBack();& z: r) r9 F$ m$ y( K
...
9 X3 `4 R2 Q, z, J mUnbind(HttpContext.Current, SessionFactory)0 l1 C* t# H. X& i0 X
1 a. O/ f5 ]! B' }. N7 k1 |context.EndRequest += CloseSession;' O2 {5 t0 _4 G3 y6 D
7 H/ Z; u# t1 j* F, q4 {' i9 l
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|