 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
8 i( ?" v7 e* S基本参数:2 G9 q0 j3 x7 _; T
jta | thread | managed | custom.Class
; D* P0 z6 u7 g* @' j主要是配合2 I0 ?7 _ [% S2 ^0 e
SessionFactory.getCurrentSession()- p n5 ^; d5 h: P2 T2 R
来用的.7 ]% r& O3 b4 w i
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
0 ~+ V3 W3 T1 l; ^Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:# ?! B( O) r7 {% Q) B, l3 d8 O
context.BeginRequest += OpenSession;; y3 E- v7 Z+ V4 I
.Bind(HttpContext.Current, SessionFactory.OpenSession())
" z+ A" @* L: Z" k
) [& u/ @, \' m/ T+ F//do transaction9 _8 l6 S% }) l ~( B
getCurrentSession().Submit();& X. g! \ a, V3 B' x
getCurrentSession().RollBack();
( W5 b0 q3 L0 \, |6 R* _...9 B! f7 u1 D1 |, F* h6 V6 ~
Unbind(HttpContext.Current, SessionFactory)
( m0 `" W! m8 ^1 y
/ w ^, O( }* C5 l5 Tcontext.EndRequest += CloseSession;' G+ J6 C2 O6 {7 l( {
9 z4 @- ~" y8 R( f- L" d
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|