 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what z7 P/ J& i+ D3 M5 u
基本参数:
2 h6 s9 G. N1 P/ a% \6 f7 Bjta | thread | managed | custom.Class
0 x+ j, h$ Y! Q. S: c5 K主要是配合& I- [+ |* l# g$ s/ Y. T1 D" d. [2 }
SessionFactory.getCurrentSession() ] [0 S, {, {3 c1 [$ ]# A( [5 `
来用的.; {. R- Y5 c, X! a* X: H
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
9 {% ]5 @- `# O8 G( c) n- ~Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
+ L1 _1 R6 J7 f1 a) Bcontext.BeginRequest += OpenSession;+ K$ t7 [2 a4 v* S3 U9 ~, S' W
.Bind(HttpContext.Current, SessionFactory.OpenSession())
3 u$ ]. h/ V9 \
6 u/ @% A; b- e. T% p# p% w//do transaction; D0 K4 P* \) T3 o- W
getCurrentSession().Submit();! x y0 g, D. e$ t5 i% U# Y+ M& x6 y
getCurrentSession().RollBack();+ c6 N1 ]8 }& F ?! e+ v$ `
...0 @: ]2 B. @- s5 L! M7 {- q
Unbind(HttpContext.Current, SessionFactory)
: V, b+ B$ K7 H* B1 P4 w
; J0 F3 F0 X0 j7 m! q" qcontext.EndRequest += CloseSession;( M( f8 Z3 @9 Y7 ]
0 o6 M9 |3 n l( g$ B" T
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|