 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what# X) F4 B$ |- X2 O9 R
基本参数:' h9 v9 \! s$ E3 h4 o+ k# C
jta | thread | managed | custom.Class, p. ~, [. \3 q* _) z
主要是配合
+ C! f8 L* ^/ d; c' h6 a1 XSessionFactory.getCurrentSession()
: p Z/ S( I! U& `3 y" V/ j0 b/ G [ `来用的.4 _+ ~$ m* k- _% J% f5 e
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,8 U q% [2 y+ C7 O" o: Y
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:$ G( V6 w/ M, D9 H, a W( e- Q
context.BeginRequest += OpenSession;& d& p6 [4 i8 {" L4 J
.Bind(HttpContext.Current, SessionFactory.OpenSession())# G) K" s5 ]- s" m$ M2 W2 n
, w2 B& \. n2 o5 }//do transaction3 E) E- t1 Y7 o; n
getCurrentSession().Submit();; t, o3 m8 C8 ]# M- x0 G9 k
getCurrentSession().RollBack();
( `; R& p% X, g, N1 b# J6 T8 y...
% j y. T1 X8 V# z9 g- |+ mUnbind(HttpContext.Current, SessionFactory)
! X" |: M" t% w0 H3 l1 @3 j+ P$ e! j; V4 B9 E
context.EndRequest += CloseSession;- l* k9 N4 { g/ \
( \) y2 B3 D% K4 L3 d$ d
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|