 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what% I, P+ a2 X& p" Q
基本参数:
9 \. O' Q) j: ^; |5 ]/ o( S1 g; r Hjta | thread | managed | custom.Class3 p+ I, ]2 `" e, b9 a; k' W
主要是配合; a* j# u$ J$ {8 _# f1 F: v
SessionFactory.getCurrentSession()2 f( x& E" B# R) s: ~
来用的.
0 Q* M; y& }/ ^) r& T0 ZThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,2 r0 e. y G9 a, U
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
- }( z( ^) L8 m7 i% X# mcontext.BeginRequest += OpenSession;- _7 X8 W/ H9 l P& F$ |! m) g
.Bind(HttpContext.Current, SessionFactory.OpenSession())" @ Y# l/ O- u; J3 m, x
" [" m! U* w/ \6 Y" W! u: D//do transaction
& P( C" f4 ^9 s4 U% O% R, QgetCurrentSession().Submit();1 E. i( c7 Y' g0 r9 L
getCurrentSession().RollBack();
/ L( |5 M4 \, B9 |8 }: X4 p. J...- v+ X( r* Z" A* v2 m, r
Unbind(HttpContext.Current, SessionFactory)
0 |2 W" t1 i8 S& Y* ]8 l9 M
: E9 {- \0 G# scontext.EndRequest += CloseSession;+ G8 E: r! C. D( u$ V
/ e/ k% y! m: e( s% q7 d
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|