 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
1 w5 J) O1 q' Q( b0 }基本参数:( E) K6 j4 |8 Y4 m$ z, f
jta | thread | managed | custom.Class
* G0 Q1 t+ R% L/ |# o主要是配合: D6 p/ r1 g3 p' {, l6 g
SessionFactory.getCurrentSession()
$ C9 S: |' H5 h来用的.
- E9 ~3 G4 x$ X* f! EThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,, T# f0 m# b* u4 V8 `# E1 W1 W
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:& s+ |3 k, G$ \' H/ G) u
context.BeginRequest += OpenSession;
( X ?8 k. X. r6 ^& ^- }0 H4 f, E.Bind(HttpContext.Current, SessionFactory.OpenSession())) r n1 i9 O, _' q
1 ~; U* a; P0 A% d& v
//do transaction
3 _9 @, t: h) p) G* {) u! X7 UgetCurrentSession().Submit();0 d- a* f" D# s% R
getCurrentSession().RollBack();
4 \" d' I; Q3 i0 E; M...
! _$ F& R% T4 U( h; u2 bUnbind(HttpContext.Current, SessionFactory)
# W% D% K) f7 E7 N; E7 O
3 v% D% T' v. ~7 U0 Lcontext.EndRequest += CloseSession;; y6 ~9 D7 d( Z. E3 ^4 \# P' v8 V
( h6 p+ ]( o6 v2 i4 N
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|