 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
5 ~( i: W I b ]/ f& F1 i+ m0 \基本参数:( \, }9 N% ?; {; ?3 \
jta | thread | managed | custom.Class4 P; v: K) F1 Y3 u; k
主要是配合* w, l+ A+ s2 _
SessionFactory.getCurrentSession()7 P& f: z V9 `1 o. ]8 s8 O: r
来用的.' E0 g1 L, k! ]/ P5 c9 I
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,' g2 n& j9 D8 K8 l0 V) a+ g. t
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:) V! m( U! i% y7 a
context.BeginRequest += OpenSession;: R/ g' G& h; I3 B# n/ w6 I: y
.Bind(HttpContext.Current, SessionFactory.OpenSession())" H5 g1 g5 [3 [6 y2 X
9 F8 G; y/ D0 `# t1 x9 x//do transaction
% ^* T5 g# a, {: t6 U; hgetCurrentSession().Submit();7 \$ k: h0 e- T# |0 y1 L G
getCurrentSession().RollBack();% g* E+ G& C' @. o9 q ~, x
...
9 k9 [3 i, Y9 z5 YUnbind(HttpContext.Current, SessionFactory)
2 g1 w1 x& q$ L! e& X) C
# Z& ?. E7 k% n! T: E! Zcontext.EndRequest += CloseSession;7 \" i4 @( R& g% ~) R
) ^; q: O( B' c; |9 W. L
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|