 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
2 d5 x) X- v4 d/ Z基本参数:. z. F* t' s/ I) y' O. F. D
jta | thread | managed | custom.Class, t8 Y2 O1 w: _7 W
主要是配合0 G! j6 a5 ?7 V+ W7 l% Y! w, v) l
SessionFactory.getCurrentSession()/ c; h- ~# _, K* l$ B7 O" O
来用的.
( k Z3 c. k: B5 O8 a; p% s `Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% T) X' ?1 F3 y e
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
3 j2 a4 e9 b: T7 p2 x* G5 X# ycontext.BeginRequest += OpenSession;. I4 j% p$ c1 X( O% W3 }
.Bind(HttpContext.Current, SessionFactory.OpenSession())( ~& N V/ D( p2 o& w; K9 Y: A
% R6 V4 _+ c+ J7 d" N- i6 l3 M. g
//do transaction
: O! G; b& c1 V( f. bgetCurrentSession().Submit();$ V) S# V2 }7 F( o5 u& _0 _
getCurrentSession().RollBack();% W1 [7 C @1 D
...
6 p% X9 j- l3 M" ~) mUnbind(HttpContext.Current, SessionFactory)
: ?0 B) K/ g" b* o2 h$ u6 Q' w3 V7 s% `7 q. e t
context.EndRequest += CloseSession;
/ u# n& F! Y8 R( [) ?- z2 T7 R, V5 j, c' G$ p+ b
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|