 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what2 J5 |9 l3 b8 d3 f+ Q
基本参数:
& O( ^2 b4 ~6 w# p; \jta | thread | managed | custom.Class
: o7 D1 T/ E k/ M; k) w( |主要是配合: k4 e0 u* Y0 ^( d
SessionFactory.getCurrentSession(). l3 p# E. o# M: y, [ ~
来用的.' @; W( E* M) L+ I; }: M2 f! B
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,9 N7 r/ x" U! G! G8 O
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
) @5 ^ d6 ^0 _* o9 qcontext.BeginRequest += OpenSession;8 U+ l( I1 W2 |
.Bind(HttpContext.Current, SessionFactory.OpenSession())0 S0 }9 i( U7 }/ l$ B# K3 A
. \: Q: `2 d1 _- t, B4 f
//do transaction& H7 }: F$ J+ k5 W- B4 ?
getCurrentSession().Submit();$ V: c4 _$ m2 t
getCurrentSession().RollBack();- \& e; Z2 W: n
...
* U7 _1 Q$ O1 u. H1 AUnbind(HttpContext.Current, SessionFactory)7 L* O2 D2 L6 K" ]3 L9 O
( m2 V/ y5 z2 Y" `/ J X/ R. ?! m
context.EndRequest += CloseSession;
* D+ x, R) r: o2 p% T2 V& U$ z: q& ?' N+ V' i0 Q" Q( }
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|