 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
" ^" Z: ~5 j9 K& @: G1 C, V0 o0 x基本参数:
, E8 m! x8 c6 M* V1 P8 xjta | thread | managed | custom.Class5 M2 y2 g9 i( i7 K# c$ J0 y) l" i6 T
主要是配合* A( q" n( e1 Z0 ~6 z U
SessionFactory.getCurrentSession()
5 E# g x$ m5 Q' F1 z来用的.
, C/ q$ ?6 C0 A* h- u5 ^, S3 tThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
' J; u& N, X5 J1 f: B0 ]- KWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:$ ]2 K( u d4 T/ `, K' R0 X. O7 \
context.BeginRequest += OpenSession;
( L: O) J; C7 T.Bind(HttpContext.Current, SessionFactory.OpenSession())
5 c& h& U: }5 \4 d! C! |* r% t3 a; k. ` M' A& v+ u7 c
//do transaction
5 b9 [: U7 [5 K, \3 }2 zgetCurrentSession().Submit();
- c* H! B0 @, V7 Q" D/ EgetCurrentSession().RollBack();* O! ]& F5 v+ h* U4 z
...
/ i5 q5 G" N4 e0 Y$ e3 a1 EUnbind(HttpContext.Current, SessionFactory)
- y0 f+ i8 W* c9 ?; ?! J6 L- R8 l
context.EndRequest += CloseSession;
- ~8 X0 c% q3 ]3 h
$ a8 P& R7 Q k, \ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|