 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
% Z1 M4 z5 \6 |4 A3 ~基本参数:
" m {) U6 x+ G/ k5 i/ L7 Z4 Kjta | thread | managed | custom.Class
9 l+ ^2 a2 H8 b+ e; \主要是配合
9 N9 n) D" `/ ? K1 t; b& g: J$ CSessionFactory.getCurrentSession()
8 j: |' N5 j9 a9 p+ q! k来用的.
3 {1 S# _0 w/ sThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,7 w7 @: k/ `$ l# t
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
* L( @" U. N/ h' l, m# e, W5 y, I; Vcontext.BeginRequest += OpenSession;
1 N" y7 B: G4 e$ ?+ I+ w# ^.Bind(HttpContext.Current, SessionFactory.OpenSession())$ b0 x* C+ g/ U8 P% ]/ I
$ ]$ }& e3 y6 b! h" ^- F
//do transaction
7 C; A6 w; Q0 l* EgetCurrentSession().Submit();
! F- m' Z1 e0 f) IgetCurrentSession().RollBack();& B' s% m2 {/ |$ n
...
( o6 R' ~7 C( p# Z* @) zUnbind(HttpContext.Current, SessionFactory)
7 ^2 \5 t& }- E% A' r, R' }4 U2 W0 [+ T! G' X* u
context.EndRequest += CloseSession;
; j5 ]7 ]: }" V1 J' e6 n6 X/ [1 Y0 H& c/ l, h$ u. o8 b9 ~$ `& F3 e
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|