 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
" z! \$ [& x& R$ y基本参数:
, }( G: n9 a# F* }) K. x% r7 K1 h4 Y" t Wjta | thread | managed | custom.Class
3 m: B2 Q: M8 _主要是配合
) ^1 h, ^8 M6 W: ^& \SessionFactory.getCurrentSession()5 @0 b. l {' w7 @3 `+ N
来用的.
# p1 g+ h! x( j W, y: d' iThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,0 [6 y1 Q9 h! m$ V
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:) U# e" V' g. o
context.BeginRequest += OpenSession;/ @1 l3 O/ |* x# A- c8 z$ Y- b( Z; V
.Bind(HttpContext.Current, SessionFactory.OpenSession())& w k" T" u1 b1 X, ?+ y5 x7 y
: Y, ~9 o- ]4 u9 L//do transaction
% R0 W$ u$ [2 W& z; F6 s7 ?* [getCurrentSession().Submit();1 R9 L& j4 j; T7 n3 E
getCurrentSession().RollBack();3 n) h1 H! ]6 j3 s% D. `9 P
...
/ q# I: ?" D( W6 M$ }Unbind(HttpContext.Current, SessionFactory)
2 n9 o/ _9 k5 G' U/ V! x& O: X- R" @& {. Q' G0 i
context.EndRequest += CloseSession;
, I1 L8 f; M5 r7 z5 T+ |
: f3 \4 L$ b" q2 A Q, Q3 T$ _ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|