 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
2 U+ s, F& R# z. J* W基本参数:( h0 w) |& p& _$ f% k, C# k
jta | thread | managed | custom.Class
; M' u7 Z7 H0 C; a0 f/ K+ c主要是配合
* {2 Y) W5 F. W, qSessionFactory.getCurrentSession()5 y8 ]2 t2 Y, ~8 C% Z, y
来用的.0 q( u {' n9 b
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
/ F7 b( B- J6 ^$ v" r$ M1 s3 y& |Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:8 p5 k2 n7 M& q: ~2 @, V
context.BeginRequest += OpenSession;: m, @+ c5 l* D
.Bind(HttpContext.Current, SessionFactory.OpenSession())9 R9 E. @7 ?3 k9 O. y. l
; I/ O7 @8 E# ~//do transaction
7 u" s- x& N& f; G$ HgetCurrentSession().Submit();
- D7 H) J) P# Z, h! Y2 ugetCurrentSession().RollBack();
6 w4 D9 O: L1 D' B* M2 _4 X$ h...
( K# |1 o6 Z5 @Unbind(HttpContext.Current, SessionFactory)5 H* _( E4 ]. H( S( K% n
2 ]" o6 C, ~ c& Q1 g J
context.EndRequest += CloseSession;) Z2 ~# K1 [, J5 o
# t, r1 J7 G/ k7 O4 r" Y% g# o 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|