 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
. G3 @6 u0 S* \8 n7 I* y基本参数:
, Y3 S+ q2 x# O- Sjta | thread | managed | custom.Class# i8 o9 q" ^- y" o: r# x/ b t
主要是配合% l! P' {4 P& {7 g0 V
SessionFactory.getCurrentSession()% @! S- L$ O* k$ I* |
来用的.2 k H3 y/ b7 m1 R
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
- g% O" ~- K( E7 l- _4 {; w2 }Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:' P$ n. P8 C# x
context.BeginRequest += OpenSession;1 `/ B* d/ b$ K8 I1 S( c8 I
.Bind(HttpContext.Current, SessionFactory.OpenSession())
$ G! c4 J$ v7 y+ f) W
: S* U# H# X; t. i& v, j d) T//do transaction' ~' N/ M* H; n
getCurrentSession().Submit();; x- \( F& E4 W/ `: j/ a" S
getCurrentSession().RollBack();
m( Z4 \% v* g* _! }3 k...2 ?) i3 X4 g/ U8 [$ p( N! |* ?
Unbind(HttpContext.Current, SessionFactory)! i3 Z5 d; V2 ]% Q, H
\. W+ s2 `' v V4 S! t" Y5 n
context.EndRequest += CloseSession;1 s4 z. w! F+ h; A6 U0 B* |
+ ~% A0 u" b; o; M 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|