 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
+ V5 V& M& I; n基本参数:# T; F! S* j9 b& E5 C, R
jta | thread | managed | custom.Class8 p( N0 P. ~- a4 N
主要是配合
7 B0 f5 R3 }5 O; q9 q v. m( hSessionFactory.getCurrentSession()
2 r, p( e! q. ~" F, @$ s来用的.
5 D3 x) j o8 P) Z( GThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
4 u; W, Y+ w. C6 f4 q( cWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
3 K3 O! s! m8 E5 g2 j1 h; B) }context.BeginRequest += OpenSession;
& b) Q3 u) e/ p: g$ I5 L.Bind(HttpContext.Current, SessionFactory.OpenSession())
! T/ d9 b7 W- Q7 n' D
8 l% I/ E" E: r0 \9 e3 t//do transaction
$ l1 A# j, `3 `( n- f0 M; IgetCurrentSession().Submit();
1 T0 O" f4 m6 f( V. N2 ggetCurrentSession().RollBack();
$ n. _6 s5 a! i4 }' V6 M: X& m...* a- `, u0 G/ [" ]0 p: [+ l% |
Unbind(HttpContext.Current, SessionFactory)
% d/ o: Q1 Y1 S$ V4 s" M* w5 d
( W$ A' C* |6 Z. [' Wcontext.EndRequest += CloseSession;
8 ]4 r+ P) T6 H% j4 [ G
2 T/ U4 L( D3 n6 A0 p- L; ~6 E$ p1 T* ] 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|