 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
% `' f! H# t# |- H/ ]基本参数:
& k: M: Y7 o tjta | thread | managed | custom.Class
+ c, p7 A* x$ g+ w8 Q- T1 v主要是配合, @0 y, b7 R* \9 `/ Q7 Q( [, q1 X
SessionFactory.getCurrentSession()
: n' d4 F M+ I& p7 ^ o来用的.' N+ ? o3 k" R5 l! G
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确, P0 f3 N, r5 u* U Z8 t4 D4 p
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
; _' X# } n2 y! x2 a; F( Lcontext.BeginRequest += OpenSession;
& e; J' P! x# P) j0 t! A0 x.Bind(HttpContext.Current, SessionFactory.OpenSession())# `! k* K3 I" L) {3 R8 v: s
; P1 o3 W0 f7 |6 e, j/ \3 }
//do transaction
2 _% S8 p0 Q% F, W3 T2 c- pgetCurrentSession().Submit();
% r$ F4 N$ D, O& O5 NgetCurrentSession().RollBack();7 d4 U* Q* E* Q" I
...
a/ q& W* L8 r1 ]" GUnbind(HttpContext.Current, SessionFactory)
[# k! ^, a9 Q% O9 |
- F7 K+ C+ O @ h/ ]& e5 zcontext.EndRequest += CloseSession;
$ ?8 G% p; t& ]9 _* Q
: T0 u/ `! y* J' b 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|