 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what7 {- s, O3 C" F- c4 H8 Q+ r, L
基本参数:6 z. u$ d$ S3 q! g- [% Z8 E
jta | thread | managed | custom.Class
: g# m1 E; ~$ H8 |, A主要是配合
9 y1 @6 P) P# f) ^" ~" u5 M- _SessionFactory.getCurrentSession()
6 @2 f* Z) a3 _" m9 _3 I来用的.- ~! z- Z9 u! x- P) s- |
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,) B4 s7 }$ Q& J2 W% E4 ?: u
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
1 b T" ^/ ^) ^/ b( _3 ]9 {+ M+ C: Scontext.BeginRequest += OpenSession;) @6 G" S8 s0 l/ M% Z# ~
.Bind(HttpContext.Current, SessionFactory.OpenSession())
! r, | E- \5 V D3 N# q( Y+ [4 q" ~4 ]" e$ }( R. b
//do transaction
8 B& o# l3 u7 i7 m! l& AgetCurrentSession().Submit(); [6 O9 z1 H* q& \! O6 k
getCurrentSession().RollBack();
& K+ c9 }$ ]6 B/ p" F6 o7 T- E7 o...
$ |; V% J2 G" |, gUnbind(HttpContext.Current, SessionFactory)
6 s. q0 |/ W( w4 y- L0 `$ v
! |4 O$ y+ F7 D, jcontext.EndRequest += CloseSession;/ f1 ]7 k4 R) ?$ _0 h) ]
9 T$ a* ?7 ~+ O7 X 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|