 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
" p$ B: }- M5 w9 g( t9 f4 s- e9 { z基本参数:
2 o' j; T/ R/ i' r+ a6 X$ rjta | thread | managed | custom.Class0 H" H0 _0 t8 Y0 h0 G
主要是配合
0 S1 m! h( R7 zSessionFactory.getCurrentSession()/ ?2 M" u0 C! r; ~7 o+ z6 e+ }; V
来用的.( ^8 C& s& O/ T' y. _
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
3 V" j9 H9 L% r) SWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
- l9 P7 O& {" I) Hcontext.BeginRequest += OpenSession;
2 @' G5 f+ C4 J# N3 J8 H.Bind(HttpContext.Current, SessionFactory.OpenSession())
+ \) u, y7 k1 G1 z' _' h
4 y$ u7 _* z! w3 L' L//do transaction
+ h2 K3 X4 N! G! k5 JgetCurrentSession().Submit();4 q1 H) t1 @" } r
getCurrentSession().RollBack();
Y0 G: V$ a( {% H..." \, ~9 b, r( K; v
Unbind(HttpContext.Current, SessionFactory)
0 T4 h; U6 u) @) b: u0 C
0 y+ `) ?: {, }3 _context.EndRequest += CloseSession;
4 L2 G- L' E* t$ ~6 S; p2 E$ y; W5 c7 S5 G* F) W9 G9 }
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|