 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what( d0 N1 O" n4 N9 A7 x4 W
基本参数:
v/ v3 g8 L7 njta | thread | managed | custom.Class
* X# T5 C9 V- F$ R3 q主要是配合5 ]" j# C6 s1 }- B
SessionFactory.getCurrentSession()! j' o4 P. i( m# f; E3 X
来用的.
8 H& J+ h4 E! hThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,! c/ y. D* B6 S. [! R% R) K; h
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:& \7 @' F9 g1 ^# Z
context.BeginRequest += OpenSession;: |1 |- v9 f0 z k Q7 P. F) }
.Bind(HttpContext.Current, SessionFactory.OpenSession())$ N* P* W. O- Q) J+ r0 e; a }
& i8 K, H2 {3 E+ s% ^//do transaction
* n4 z6 A6 g0 c& m: P% s2 R, bgetCurrentSession().Submit();, E( g9 ?* y$ _7 t; r" R" I
getCurrentSession().RollBack();
% E" Z6 q4 u. I, l5 H...
% a* z* \( K" T6 x5 J( a$ xUnbind(HttpContext.Current, SessionFactory)
$ _# K8 v6 \$ q; Q5 N* y) k" S% s x( f h4 [7 N' F* [) z+ I/ b* N
context.EndRequest += CloseSession;0 ^7 q/ w/ A: d: w) Y( L
: z$ e% l8 ]4 G5 y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|