 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what! c! B+ `% g+ I4 y# Z4 X
基本参数:4 E: f6 p% \5 L! @
jta | thread | managed | custom.Class1 Z9 O$ o, r0 q/ |& `, }
主要是配合3 P/ Z7 {' S' x1 e$ u' E, C
SessionFactory.getCurrentSession()' U4 `5 w/ O% y4 w! U6 z
来用的.
& O! a0 f9 e2 D5 _& ?& D4 @, Q' |Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 ^. ?: d# X1 c4 C
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
+ G/ a9 C% u4 c$ ncontext.BeginRequest += OpenSession;
; s6 ]" V5 s8 a' w- l% A9 l.Bind(HttpContext.Current, SessionFactory.OpenSession())9 S. ? i' e0 c. o! R& N5 ~
6 \) H4 n5 F l: ]; b
//do transaction
5 p" Y, N0 U8 `$ T+ E. k; tgetCurrentSession().Submit();
/ t/ i/ ]; ?0 u* J; p& C; QgetCurrentSession().RollBack();
% y) l* i$ \* \8 e% x...* I' ~3 \$ P& h% R/ U- x' Z+ y
Unbind(HttpContext.Current, SessionFactory)
# J v, d) u" F) O9 e- r& P5 A$ O4 c, Y' B& m$ ]5 t
context.EndRequest += CloseSession;+ c% M) ?% H0 c2 z
7 x A& _( A: h0 r5 B8 c3 k, _/ o
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|