 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
! Y% R7 W3 e. N6 C- y基本参数:
8 }. W1 ^0 F4 a. ?1 J, y. D- w# fjta | thread | managed | custom.Class3 Z7 w1 E: g8 r5 x5 P {0 e
主要是配合
6 q$ N( Q! m& h6 F: kSessionFactory.getCurrentSession()- V* X+ y" J7 n. _( `+ C* C
来用的.
$ X4 j+ f9 t# s- y. D+ V d- cThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
: j, j# ^2 C# A+ u _2 }Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
1 e) T' D- T, {) q$ x0 ^; D/ Ycontext.BeginRequest += OpenSession;+ g3 v# t( B4 _! q r# b6 K
.Bind(HttpContext.Current, SessionFactory.OpenSession())
! \5 ~" @7 h+ }
$ f$ A4 S3 F* X1 n//do transaction
8 G3 Y7 t4 h1 L# K1 QgetCurrentSession().Submit();
( Q# B1 \+ ]/ \- L7 ~7 N9 i3 xgetCurrentSession().RollBack();" j; r) G# M6 v) {8 l+ k; k4 ~. v! ^
...- \9 g& T! l- u
Unbind(HttpContext.Current, SessionFactory)" e. T* j% \. S: ?& P& Y. [
7 ~" e/ y, p3 c% K# Z. n- a T& t
context.EndRequest += CloseSession;
7 x1 E$ @8 k& L- q" B$ n+ a
% I" h, W6 l0 X1 i( v8 Q 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|