 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
. S- Y. K" d: Y9 G6 J3 z基本参数:7 p( ]1 Y3 q& u$ O! \ ?4 E6 T
jta | thread | managed | custom.Class
9 o6 P3 k9 d- U+ a# Q8 b主要是配合
# ^! l3 N% s$ Z. KSessionFactory.getCurrentSession(). `( Q6 T# ^7 R- ]# v$ x: j- P
来用的.! ~5 T& T$ J* }# c. `- O' T
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,) `1 ^+ I2 b/ J. u* o' W3 F! X
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
6 [ L3 ~3 L: Kcontext.BeginRequest += OpenSession;0 C- H; s3 H& X- G
.Bind(HttpContext.Current, SessionFactory.OpenSession())" W: o h; R, o
, ?; y9 d7 J( @% {9 J Y/ A6 m" D//do transaction7 y/ b4 b8 u/ }( H: J
getCurrentSession().Submit();% O% V9 }, m) O- Q, J
getCurrentSession().RollBack();
y% A% J- D& t/ ^! Y2 |/ f: l: l...) a8 M, Y0 u' ~ X5 z/ w
Unbind(HttpContext.Current, SessionFactory)
( v& `- ~5 y1 m& O7 |! |0 j4 N8 w1 t
+ k4 x, N' D: h7 @context.EndRequest += CloseSession;
# A% _( w1 q3 C9 J2 g$ f+ l- R5 F9 T& V5 |- \9 T" D
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|