 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what' {7 R% Q" J5 S* j: ?8 g+ l
基本参数:! ?% u; e. @' d; k1 I, V! H
jta | thread | managed | custom.Class
\+ u- V' t' c6 I3 |7 T主要是配合 I* ~* ]9 y1 k3 z/ a1 y. |
SessionFactory.getCurrentSession()
: V! {" k* P- o* f, c" C' ^来用的.; ?4 X* X! V! x5 }" }8 R
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
2 D. k8 h4 N8 y# U/ ]! _5 nWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
3 m; T9 P* v4 x& ^context.BeginRequest += OpenSession;0 w; r. H) f: x0 h/ {
.Bind(HttpContext.Current, SessionFactory.OpenSession())1 ^ o3 d) t d/ o9 R+ K; v% Y
: M: g( e6 T T/ P4 U; U//do transaction2 i' `5 U+ u% Z8 z
getCurrentSession().Submit();
( c) x1 J% G' ?getCurrentSession().RollBack();+ _/ ]! J& t5 b9 G ]
...
/ a; t$ m7 X0 c/ SUnbind(HttpContext.Current, SessionFactory); a) \5 r, ^/ |4 r" Z- {( ^6 k
0 ~# B7 G; S1 Q
context.EndRequest += CloseSession;
8 q2 }, r6 J5 h' \5 Q' s4 ~' A _8 C2 G' r4 q
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|