 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
" @8 @/ D; t% Y7 P; q. v基本参数:
, @& O) s O* x" A6 Ujta | thread | managed | custom.Class
& D% T) [9 n8 x$ n, r8 U+ w& ~: V; `' k主要是配合
$ L' c9 b) h0 j5 A9 U8 `7 nSessionFactory.getCurrentSession()
# R3 ]" A+ a- F! k0 a0 U' `来用的.3 e+ \4 D: m+ F7 Y% }- `% g
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
$ r3 S0 b# j% ?. N: t% W) NWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如: j9 L+ E+ P1 D. ^
context.BeginRequest += OpenSession;
) a$ d' H* y+ N! e6 x/ u; C.Bind(HttpContext.Current, SessionFactory.OpenSession())
* H, k" o( ?& Y$ p8 F# e* s: E& o! j2 `0 G. \7 @
//do transaction
1 ?0 C* g$ \7 N( L0 }9 x B$ rgetCurrentSession().Submit();! |, j0 h# w# |; e
getCurrentSession().RollBack();
) b- E. g2 D' g! I: m# H! Z...4 m- D. |; G3 x [+ E% N6 K
Unbind(HttpContext.Current, SessionFactory)# D* q& X. |9 P! F
- [" [ x- T* `% t
context.EndRequest += CloseSession;* g' r b) C' }! F
4 q/ f+ R& R, E1 f5 a
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|