 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
4 X& m6 K& q4 b7 q R基本参数:
0 Q5 |/ o: Q0 ~6 Y" t. H2 [) y8 m8 Ejta | thread | managed | custom.Class0 S" @. O8 @+ |
主要是配合$ V( S& X" T* t) M& X
SessionFactory.getCurrentSession()* G, }# x- `. a% x/ Z. _
来用的.
1 S. a3 S" D( mThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 Q6 x1 u' V5 |2 K
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
" h+ p" u; k" Z$ n7 V9 S3 ?context.BeginRequest += OpenSession;7 p+ t7 T3 O2 N/ r/ w
.Bind(HttpContext.Current, SessionFactory.OpenSession())
7 P. g& f: g% {8 {( K' U( }7 {( B6 l8 u: M
//do transaction1 p& G' u! M* S) m" s' ~ s7 Y
getCurrentSession().Submit();
4 |8 |% w: K) B3 I) d, o, x2 F% BgetCurrentSession().RollBack();
$ e! ^9 f2 a+ N( a3 P+ ]% l" n...4 e; G; Y. z2 l5 F
Unbind(HttpContext.Current, SessionFactory)" H& l' u# V8 x9 H* W9 C
$ @- ]/ p6 V% J8 dcontext.EndRequest += CloseSession;
+ c$ c( I0 f( l# X) g" W4 f) W) `2 {6 _' N/ Z8 V5 M
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|