 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
2 W2 j. r3 q) ]" T$ r基本参数:
, P0 t2 ?$ |5 i, hjta | thread | managed | custom.Class; w$ x; [- \ m$ S
主要是配合
% z! b0 \4 x" j8 F6 ~( s3 jSessionFactory.getCurrentSession()* d. m4 m# ~) S1 t. R
来用的.: B3 I8 Q% b! Y
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
% G5 E$ C* V5 Q _1 UWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:$ ~" Q% I$ |- q8 u) x
context.BeginRequest += OpenSession;
2 R" z0 j& l+ W.Bind(HttpContext.Current, SessionFactory.OpenSession())
9 G8 a0 [ c( M; |: ?6 E$ d. p
/ V8 ]+ V" t2 ?4 p//do transaction! |/ b# r' M3 r. x& T, L
getCurrentSession().Submit();
1 O6 R, I2 h; sgetCurrentSession().RollBack();
# m4 M, c3 u3 S( }...4 T: L. E) ]+ G( Q! \7 M4 v. U6 L
Unbind(HttpContext.Current, SessionFactory)
( f/ k1 m/ n. X( z6 _" M( x
4 A9 o( t( |3 a8 |3 A/ r/ }3 Xcontext.EndRequest += CloseSession;& I5 G* g0 W0 ?2 E' X. C# ~' r
! V: J: v6 O% E" r7 G! e2 ~ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|