 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what& I+ R) p4 N/ u( `
基本参数:
4 M1 S, e" s& I$ n- p1 n7 Bjta | thread | managed | custom.Class
; T) A- K# w6 Q w主要是配合, D. V! Z: m/ e* B# R2 c
SessionFactory.getCurrentSession()$ N6 A% ]7 d- {$ l( A6 C
来用的.
: Y* [$ A4 Y6 l1 M7 y; e/ ]5 Q, YThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确," k+ }" x. [$ W( q4 G
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
# |8 g5 [3 T4 y* Hcontext.BeginRequest += OpenSession;8 }& t5 h* D6 v: h
.Bind(HttpContext.Current, SessionFactory.OpenSession())
( U7 ]) u: D* R, N" K: `8 j
. ]# X+ T" p* @//do transaction/ R3 ]- G+ p& k. A4 w
getCurrentSession().Submit();4 J6 G/ ~+ b4 J f" ^6 |
getCurrentSession().RollBack();
) p! t; M+ o+ Y* c* R- l...7 W0 l9 L- s; z8 b+ Z/ W4 Y
Unbind(HttpContext.Current, SessionFactory), H; S& c2 v. [: s* A6 i
* B/ W( X9 M# h3 k# J0 P; ?( [context.EndRequest += CloseSession;* a) I* Q$ c4 U; R l
! O) z6 P+ C A6 m1 H5 T
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|