 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
+ y4 c' Q+ \4 d- i! L. ^$ W基本参数:
7 f/ V0 N$ C3 x( M( }jta | thread | managed | custom.Class
n" i: A: I1 H0 C主要是配合2 e: N& b4 ?( t' u. ~7 s
SessionFactory.getCurrentSession()
1 j3 j, h% _% ]% g- j来用的.! K1 _+ K c* N1 i- ^! X
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
% @2 ?' d) T! W& h& C4 B' b& X+ \Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
1 f+ N7 E0 O [& P1 Fcontext.BeginRequest += OpenSession;
) x z9 B: }) k+ B: `5 x.Bind(HttpContext.Current, SessionFactory.OpenSession())
2 C" Q' F& B7 u; \9 G! j) q2 o6 C6 u' w# s, K- O
//do transaction( J3 O5 o( }5 |: S3 t
getCurrentSession().Submit();- ~9 k1 Z' X d' v1 p5 H
getCurrentSession().RollBack();# Z6 ^% s4 R) y" ^9 Z* x
...
4 M, w( r- r8 l4 l; c3 J l) dUnbind(HttpContext.Current, SessionFactory)* m, {; J4 e5 V! D( E1 s
5 e ]; e! m, Y- O$ I' O8 qcontext.EndRequest += CloseSession;
6 |& _2 T$ y) E$ A
# a3 \- k# h. A 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|