 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what1 {" f W, S3 W! D( u
基本参数:
9 L+ l" u7 y1 u( `; Bjta | thread | managed | custom.Class+ Z8 ]8 X; m6 @9 Z
主要是配合4 Y. ~8 z3 m# m. e2 P4 O
SessionFactory.getCurrentSession()3 W: d3 k" j% M2 C6 N
来用的.0 w. u4 R7 y2 F% w; c. d
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 ~+ \/ R9 |; N% L) X
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
9 _4 ^2 n( h/ c1 wcontext.BeginRequest += OpenSession;
( L! l! e' i. `( z) S6 d.Bind(HttpContext.Current, SessionFactory.OpenSession())
9 O0 f+ B# u- c9 G; [5 R: }& ]9 f. x2 |& D, J
//do transaction
, r+ M! E, e3 J+ U2 qgetCurrentSession().Submit();
7 Z: d. C) i2 Q2 E4 agetCurrentSession().RollBack();
3 b* p9 }* r- i...) K* j) R$ ?! X. q0 v! z
Unbind(HttpContext.Current, SessionFactory); H+ ^* U' f5 {' ~ P1 Z" s; x
9 a& V" W; L/ M9 G: v7 l5 l
context.EndRequest += CloseSession;, S1 w% \& _4 | }
; c" t# x; j' c. t! Y; @/ T) A
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|