 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what# a V) A- {% m; b U9 n
基本参数:; |2 P% @4 {4 C8 @( |& F
jta | thread | managed | custom.Class1 ?# Y1 |8 g2 [
主要是配合2 E* f4 L. {# l& y
SessionFactory.getCurrentSession()
% J7 n' R$ X" A- R, ^来用的.4 E5 ^" E* h# ~8 [- J/ P% f+ W' h
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,, V; o* D+ ]0 r6 G+ a0 b
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
, w1 y: M& j6 a0 I. V* jcontext.BeginRequest += OpenSession;
8 q! Q8 L) X. d% r) j; ].Bind(HttpContext.Current, SessionFactory.OpenSession())
% a y( ^0 q1 e6 `2 }
! k# }. n+ }8 b) A" R//do transaction! B/ O. {8 E! [% L! M8 D
getCurrentSession().Submit();
4 h) ]; D$ J9 HgetCurrentSession().RollBack();+ I* G* Y$ B$ o9 @
...; n* c1 ~! E3 D: M, H h. c. R
Unbind(HttpContext.Current, SessionFactory), I$ P6 q% F" r* x: p5 w
6 ~: L) W. O) G
context.EndRequest += CloseSession;
) [1 u ^. M# k6 k% A7 x$ r4 f# I; ^
% B" y# T. D8 ?+ f3 t9 k( _ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|