 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what& k( k4 T) _& e& M h/ O7 v
基本参数:
\* [( k; @5 q/ w9 w0 ]$ A2 ~jta | thread | managed | custom.Class( K) K; }" i0 ?+ x
主要是配合
4 L' a, f& o3 c" }* y6 q( x/ ]SessionFactory.getCurrentSession()
/ A* g- O' n0 N# U- `' ]% e来用的.
& `; K2 M8 _6 r/ ]/ T$ C" U& i1 }Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
: b4 I! i6 A; l ~5 vWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:* \& M+ }! k: ^. o7 V6 c
context.BeginRequest += OpenSession;
0 D" W5 @% M8 ~* f, H/ J3 o( A) Z.Bind(HttpContext.Current, SessionFactory.OpenSession())1 a! o5 A! K: Q
- r X! g9 ?5 B8 [+ Q7 z
//do transaction- a) G4 l- {+ R( f; `0 B% `
getCurrentSession().Submit();7 D, F7 y2 _8 |3 }
getCurrentSession().RollBack();( a. W5 J, }( z
...% u5 x+ X7 {6 K4 W% f# e# u7 {
Unbind(HttpContext.Current, SessionFactory)6 a5 M4 g1 N4 l, g% b
$ V W( Z/ H0 r" ]0 k5 z
context.EndRequest += CloseSession;" l- N2 a# Z& O5 e, S/ \, h
* i8 t7 F( V7 V( k* D% f5 R
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|