 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
B$ S/ w4 T* i+ k$ n2 q( ]2 m基本参数:
1 ^! P9 h) }, d% J( ajta | thread | managed | custom.Class
$ ^* W v6 V- y/ V7 H( `7 q主要是配合
3 `2 |$ p8 S) `, `: CSessionFactory.getCurrentSession()8 c" Y F3 D( y+ s7 S* D P
来用的.) s& s Z( x4 S+ I
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,2 z6 W6 Q9 m" i3 @' p
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
, K/ }8 j7 s' |4 }/ B# F0 Ucontext.BeginRequest += OpenSession;
# m* M% {$ i# d4 y! S9 Z' H.Bind(HttpContext.Current, SessionFactory.OpenSession())$ @: S6 c) A" w+ |$ T
* t7 E( u; w$ ]5 \2 q# O' B//do transaction& d1 t, |& i# X9 d7 z! Q$ @5 E2 |* z
getCurrentSession().Submit();- K) r% L7 p6 a) n
getCurrentSession().RollBack();
* T; L6 ~' P- p$ K! M...: ^2 [/ x: }; X% |
Unbind(HttpContext.Current, SessionFactory)" K5 a; T: B: U; `. A
- K( e. p/ M3 v6 Econtext.EndRequest += CloseSession;
5 f) W6 s5 J$ Z: _" O6 D9 U- Y/ U* s8 J1 i* q
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|