 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
- H& W9 g, R2 U基本参数:
$ c) H r: |- e' P" bjta | thread | managed | custom.Class
* ~1 V: _% ]' |5 c( y8 \! ^主要是配合
) N) `* i& ^% ~9 i& f5 d8 u3 xSessionFactory.getCurrentSession()
" B5 t* G U# l1 k$ I4 I* `来用的.- _4 d' o( Y) f( W9 B
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 R- M* N# C! n) v+ V% i/ b! ?. E8 T
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
( S' N# k( b# ~! ncontext.BeginRequest += OpenSession;
% n. J% }$ b1 h9 E; e.Bind(HttpContext.Current, SessionFactory.OpenSession())8 _6 A+ d( B' ?
% a9 n4 j1 n* K4 a4 ]' E: E: I4 i5 X
//do transaction
: X7 g4 J% b; l9 \2 K0 |" PgetCurrentSession().Submit();0 X% D5 h9 r% T- |! ]
getCurrentSession().RollBack();
- b+ @0 m2 G% d, e7 [; G...
: T9 Y( n* i( l+ m" p! s! F( {Unbind(HttpContext.Current, SessionFactory)/ G7 S' q' W0 U+ P% `+ a0 Q
( Y! `3 @8 e: J! Acontext.EndRequest += CloseSession;" v! a% Z8 K/ h; O! D. C
4 g! J( R& u# F9 r8 Y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|