 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
) n+ ~: l1 Q4 P7 h基本参数:
! ~ N* J3 K( T; hjta | thread | managed | custom.Class+ t2 L. o& O6 K( ]3 P' L
主要是配合( ^( R) S8 p; A/ R
SessionFactory.getCurrentSession()6 Q, o9 p3 O' O/ _. F" u3 a; l
来用的.2 T( ?- ~6 X, K% c4 z, n
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,' W! J- a m! z3 @1 ~1 n: V1 [( d
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
' e: Y2 z2 ], W1 scontext.BeginRequest += OpenSession;
5 K Q3 Z* e; V! d( o, @8 | S' N.Bind(HttpContext.Current, SessionFactory.OpenSession())0 M" Z* x9 u9 V$ J3 q
, i0 i1 k b* }2 {//do transaction: B, o3 m8 ]. }4 L4 I1 u8 {& a8 [
getCurrentSession().Submit();' f. u: w7 A0 [# q" A: s- l. z
getCurrentSession().RollBack();
6 W+ ~5 A' x( s5 [% a K...
6 l3 E- u( N. ~Unbind(HttpContext.Current, SessionFactory)' Y- C6 {2 W% z$ v/ m
% N F" d$ X( Z& w
context.EndRequest += CloseSession;
7 `, i5 U0 D* v$ k5 j4 z: X" C: @! |0 ?
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|