 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what5 v7 @ [* V% L4 b2 ?' r
基本参数:
$ e+ L! I) I, P5 xjta | thread | managed | custom.Class
+ X- y. \2 `+ f' j0 {# F- M# s主要是配合0 a; o$ ^# u1 @, [5 t# I3 ]3 U
SessionFactory.getCurrentSession()
( x5 X7 j+ p( s2 p; t$ D2 _) T来用的.
) g: ?$ j$ B1 W. N' R$ v# qThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,1 c% q7 K" I0 J' s$ y* y$ j
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
7 ]( j+ L; o% n) Y+ wcontext.BeginRequest += OpenSession;. e2 P5 R% p R9 G* W, z: E! ^/ g3 s
.Bind(HttpContext.Current, SessionFactory.OpenSession())# a# s# ?( p8 @# q+ u
0 _+ M2 B0 n0 f% \& K2 z# l0 l//do transaction. u/ a+ Q, P R1 r: G, K
getCurrentSession().Submit();
f" ]0 k; p' g; e/ H2 H3 BgetCurrentSession().RollBack();
1 O$ |- Z2 T" j- L& v...
( L0 ^( e; g$ H# I1 OUnbind(HttpContext.Current, SessionFactory)
; ?5 j( z% g3 n" {$ _; K' i1 U; w. J! j; o W8 |* \
context.EndRequest += CloseSession;
0 c4 v+ c5 W1 N" q- m( i; Z) Q4 P( @9 m
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|