 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
2 X# b! t' H, D ^9 ^4 K基本参数:$ a% L# h7 s; E, T
jta | thread | managed | custom.Class
$ s- q- ]: s! K! Q3 }' A) J主要是配合
% Q2 |. S8 A* W; t- P. XSessionFactory.getCurrentSession()8 |/ G8 x+ Q5 S# ?6 m- S# e
来用的.% B8 B7 L' r2 j% N9 ] G4 I
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
6 Y" L/ Y* S7 I4 C o+ z( TWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
9 a+ a4 x: P' G0 vcontext.BeginRequest += OpenSession; B. h8 o5 n/ t; E! f
.Bind(HttpContext.Current, SessionFactory.OpenSession())% G( v8 j; l1 h0 K l
' y" {2 g7 C, w7 e, }0 Q2 Y5 f o//do transaction
' n' G/ }( ?8 @3 z8 _! D& p" _3 TgetCurrentSession().Submit();
3 s, U6 i% |" X& o, F5 m7 UgetCurrentSession().RollBack();! k/ [4 y3 E$ l
...
7 t+ {0 h8 m' M7 I) hUnbind(HttpContext.Current, SessionFactory)% F+ N1 e" o$ ?4 E+ x# u5 u8 H- g
~" s0 K6 f/ s) _6 |$ p' Rcontext.EndRequest += CloseSession;0 q; w5 N1 ~+ g3 C. h
$ {0 ]1 [( e' }2 j9 N8 S$ [ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|