 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what1 @; r2 M6 @2 Z4 v2 U/ l
基本参数:
% s) k: s! U, J1 ` n, U+ ejta | thread | managed | custom.Class" K* _- p5 q# _' @, T
主要是配合
- Q5 a3 h: X r% H: `SessionFactory.getCurrentSession()
Q, G- M8 P" L& r/ C+ N# ^) |" S来用的.
9 r4 R; j1 P0 v, [Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,/ v( F/ o/ v9 ?6 w/ u: G* O2 o. k, ? N
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:' L Q, k- X+ d! C, F. ]
context.BeginRequest += OpenSession;
/ W7 a; Z: b( v.Bind(HttpContext.Current, SessionFactory.OpenSession())1 {% N* a9 }0 F N" v9 X& N% a
0 p& v2 h% q9 C2 F, p
//do transaction3 a4 u: {2 }5 m( K/ L" P$ U9 f
getCurrentSession().Submit();
) V% I \7 _4 o8 o4 OgetCurrentSession().RollBack();3 u/ }$ V4 F& u1 H* ]
...
6 V& U, p5 p3 a" a) }Unbind(HttpContext.Current, SessionFactory). ]4 Q, G' W! J4 U+ c2 w$ T
! `) L3 P* ^4 y; g# n* Hcontext.EndRequest += CloseSession;
6 |9 h4 a. {6 y: L& x3 u
) n( `& w; T1 J( x0 K$ S 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|