 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
& H+ K8 F* a: Y2 b* K基本参数:
0 M7 h9 U6 h* [7 Ujta | thread | managed | custom.Class
; K+ u- \( b6 M' M' c9 n/ E8 M( v# `主要是配合
7 c% ]+ [8 D: f4 l; l, Z$ u+ nSessionFactory.getCurrentSession()6 w, W+ c( i4 w# f/ v5 t0 A- k% A
来用的.% P# T, B, a* B- X! M
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,. O' e& d/ V0 a
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:/ z4 r" J+ U2 Y/ O& k: U/ x
context.BeginRequest += OpenSession;
' v% h; h& b5 Y.Bind(HttpContext.Current, SessionFactory.OpenSession())# L) E2 d& j; y' W6 q1 W
8 D% E j e1 ?: z) A' f
//do transaction$ N e- E) |0 E" k
getCurrentSession().Submit();* Z3 \+ e9 ^4 f9 X. V
getCurrentSession().RollBack();& E8 J9 k& q+ x# N+ z; ~
...) o! _- z; O, A+ [% p; {
Unbind(HttpContext.Current, SessionFactory)
+ W# D6 m/ F" s7 j; Y* ~
% c) j( [0 S/ t) q) x) A) r% Vcontext.EndRequest += CloseSession;
/ X- T2 v* D# Y6 T
, l7 N; K2 U) y 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|