 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
. B9 R, F2 Y9 s: e基本参数:
6 W2 l; _! z; ^: wjta | thread | managed | custom.Class- y* `( i6 |8 n, H
主要是配合
8 g; y: R( ^ tSessionFactory.getCurrentSession()! ?4 `) x* K4 L
来用的.4 w* V7 g' y* Z* ]1 K
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,1 ]2 L7 ~6 l1 o; S0 h
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:$ P4 t4 W$ U' c- D
context.BeginRequest += OpenSession;
; J/ e) W- `0 \7 t5 V z.Bind(HttpContext.Current, SessionFactory.OpenSession())$ |: |. L2 i' J' }* D- [4 P
- C4 J6 c9 k, _0 p$ j
//do transaction8 z& ~! Z0 Z o- k" M2 [. u* g
getCurrentSession().Submit();" B2 z$ f w6 O3 d
getCurrentSession().RollBack();/ a3 z! T: O! U, Y, n, b
...
3 Y# r9 N, S) i2 `( IUnbind(HttpContext.Current, SessionFactory)
2 K' X+ p y" {. Z6 u) a/ }
$ w. I# f6 Q6 J- L7 ~context.EndRequest += CloseSession;
- Y( K- B0 T m2 F) w: ^- N
5 O: }; I( A q4 i- ~ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|