 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what$ l& m1 r6 a0 Z9 D
基本参数:
0 @/ S5 m- k7 ojta | thread | managed | custom.Class
8 ]+ _0 Q R; Q" r" R" O9 ^主要是配合
3 u- V1 A7 R# [SessionFactory.getCurrentSession()
4 u* M T/ }+ Z. @7 p6 E1 a来用的.: p3 x }# X/ w& a. I/ Q7 Z; C7 a
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 l& n _4 r& |5 g& t+ e
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
( A; L. G3 J* x; ~, ~$ @. Acontext.BeginRequest += OpenSession;
) Z2 U0 i; ~; s. \/ T.Bind(HttpContext.Current, SessionFactory.OpenSession())
* M& h0 J" }( V1 v' g- j6 Z2 ^- G: N% P0 Z) W5 B
//do transaction
" K& q8 V r* S f7 S5 ?! zgetCurrentSession().Submit();7 q! k4 e; ^5 m% C7 V8 v
getCurrentSession().RollBack();
' Q9 k, C7 a3 {: N...
) h/ h. R) z* f5 [/ v9 CUnbind(HttpContext.Current, SessionFactory). N- z+ g" m0 x s& n- {6 r
% e- Q( h* K* c- d
context.EndRequest += CloseSession;& R$ v, @! k9 U; e7 i& V
: r1 v$ L( Y5 P. X2 K
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|