 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what' B% Q! Z6 F9 D# m# l
基本参数:
0 c9 [! v2 s/ v! n5 E# }jta | thread | managed | custom.Class
0 w- o* W9 N$ z9 q* y主要是配合7 ^+ u" }" N9 C/ _, ]
SessionFactory.getCurrentSession()
- G1 @! z( l- e2 i: j' |来用的.
! H+ o8 x% M; l- m1 A& UThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
* w/ D- f7 `; h o4 iWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:/ c4 a) i- \+ d9 l8 C: ?
context.BeginRequest += OpenSession;
. a* \: Q$ ^& W5 w9 U9 n! G5 j* F.Bind(HttpContext.Current, SessionFactory.OpenSession())* j. ]8 S% }, Z: s, ~- E! s" a' }
& ~& o7 Y+ {9 |0 S) ^0 r//do transaction. A4 p. Y: l7 v
getCurrentSession().Submit();
" E. N# D9 @, ^+ x* wgetCurrentSession().RollBack();
}7 u7 s: O* Q( q: n+ g...; t2 f+ Z5 u& S* o- g% H
Unbind(HttpContext.Current, SessionFactory)# d4 Y& r3 T" s$ y$ E r
5 r* V7 t5 j% \# B( Jcontext.EndRequest += CloseSession;
: H" ^" E- z9 `# j. }. o
2 _& O, K0 X! T4 J5 h# h. I 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|