 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what2 R. W2 b$ N7 {% D: i
基本参数:
& c- P- g; h6 T( G! W6 Gjta | thread | managed | custom.Class
. x, \7 b* v& a* C主要是配合
( Z' P* K% k! u3 F4 |+ |5 j* iSessionFactory.getCurrentSession()! Z( U* f2 q9 v1 l
来用的.2 d5 n F* c+ T3 }
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,) _; `% i" m" P B' {
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
: k( O: T) B8 q& Y0 M& z3 wcontext.BeginRequest += OpenSession;
! r& v: s+ K. R/ A) `.Bind(HttpContext.Current, SessionFactory.OpenSession())
5 U& z: ], i. T; N9 H1 Y! M1 ^4 P
//do transaction
" p+ {; @- ?* ygetCurrentSession().Submit();
& _- q4 n" U+ `" ~2 U2 f6 G% igetCurrentSession().RollBack();
6 \' i% P' P3 [7 r$ y...
X5 Q5 s# s+ K+ M: Q. RUnbind(HttpContext.Current, SessionFactory)
: `2 g1 _& {. `+ ~+ r" A5 K# Y6 T; a: j# h+ m- ?* x
context.EndRequest += CloseSession;" n: R: y5 l+ Q3 t' R/ T
1 Z/ q5 V- ]+ t* F/ g9 e. v$ X
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|