 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
! U: [) r0 }. V. _$ A. J基本参数:
2 S0 O' F6 d9 Q3 @& C' t G8 D- ~' tjta | thread | managed | custom.Class5 W, r. A/ K; @2 I
主要是配合
! k, c" \, _" ^- t0 o* {: S! XSessionFactory.getCurrentSession()4 a3 J0 I' U% i
来用的.
6 h f% ~" t1 `5 V, B8 r7 cThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,+ J) F4 w3 P: s( |
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
" A# d% K% C8 wcontext.BeginRequest += OpenSession;
7 k- ?" l5 ]$ B.Bind(HttpContext.Current, SessionFactory.OpenSession())0 d5 A) T/ A `- i; Z _
. E- N$ |* p2 x- C9 e6 X& q
//do transaction8 G( C/ r9 m0 `4 x- f- @
getCurrentSession().Submit();
5 v2 L+ f! d }# R F. ]( hgetCurrentSession().RollBack();& d" d, r; q4 s. G+ V
...
/ L1 T8 B, @ p0 u+ ?Unbind(HttpContext.Current, SessionFactory)
4 x1 k" _" E4 ~5 @. `7 O; ?- @4 D9 p) Y% p; H( G
context.EndRequest += CloseSession;
' C& a% y) p* m" W7 x8 {- c3 |8 L8 W% t
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|