 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what4 j4 S$ z2 n' l8 B: F2 p
基本参数:
5 C& K6 T# z- u8 j' Jjta | thread | managed | custom.Class
$ V# a0 B/ B+ O/ ^主要是配合4 j( h& C! C8 o+ |
SessionFactory.getCurrentSession()$ Z, g3 t" v) u
来用的.
6 T" s1 o. {% `5 t. r. g$ yThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
. x8 k1 e) o7 u [Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:6 d0 l% o/ @9 Q6 o% ]' D
context.BeginRequest += OpenSession;4 n: `& h4 _) T. Y" u/ b
.Bind(HttpContext.Current, SessionFactory.OpenSession())
6 }4 i, b* Y% J
$ f& d A6 X1 ] G3 k7 E: a//do transaction
; ?, s+ K" A( x+ P0 l3 @( Y igetCurrentSession().Submit();
9 q7 p+ _' @+ _2 E" h r1 xgetCurrentSession().RollBack();: o1 Z6 _( d2 P- m9 U
...
6 [3 C0 w& M- J X* `& J7 EUnbind(HttpContext.Current, SessionFactory)% C2 C, J/ ]! l5 F/ V$ k9 d
; M, c0 q: w$ G) ^* s
context.EndRequest += CloseSession;
# v+ o& x# j' e" p0 o8 l9 z) [- h3 } m" G, m H! S
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|