 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
# z+ A, z; ~+ ^0 ], N/ q基本参数:
1 q: _ K& U3 Z" Y4 G* L0 fjta | thread | managed | custom.Class
& M, I: w8 j0 n5 |& l3 V5 B主要是配合
& i. a' L) `# U3 |# ]4 tSessionFactory.getCurrentSession()
( u n4 ]; S) @8 o9 r# F! D来用的.8 q m* n4 z N9 ]: B
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,: m: J- n& Z! m* g1 L: H; x
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
5 v& |* \: d( l6 Acontext.BeginRequest += OpenSession;3 v+ n# n u# j
.Bind(HttpContext.Current, SessionFactory.OpenSession())+ a: z- Z9 [& i9 Q# c
8 l/ |! \/ q2 j& ^- V//do transaction* F, j# I4 @0 m8 A
getCurrentSession().Submit();
5 x2 T$ A2 w; D; [getCurrentSession().RollBack();
; L; ?6 [% C) k( c* o7 r( K' w# S...( e o2 Y5 X( p6 K5 g; [, {7 c
Unbind(HttpContext.Current, SessionFactory)/ |3 |. a0 B( V! H d2 Y
" R7 D' |$ G$ O( k
context.EndRequest += CloseSession;. Y8 o$ q# g) R: X' P+ y/ e7 h6 w
6 ?# K3 o' d9 @9 N
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|