 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what% s4 b8 X% l# E- R( N2 A5 @; J4 a/ u
基本参数:
2 N. l. D/ K/ ~. n9 D2 x5 z: Wjta | thread | managed | custom.Class
3 J, o% A) I2 B: N% _1 y3 R# h主要是配合8 N7 ~: A7 w! c0 s; A. I& ]+ E0 }
SessionFactory.getCurrentSession()" u: M4 f. S& j0 |% u: q
来用的.
, r, R, a' v) IThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
- j2 v) L8 U- oWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
1 T+ t/ U W& t: f9 dcontext.BeginRequest += OpenSession;
0 [& J. k; z; s$ x$ p.Bind(HttpContext.Current, SessionFactory.OpenSession())
, q0 z2 h; @% D: g# q8 o
1 d6 S& `$ K" r1 q//do transaction2 n; e- R9 g: b4 ?
getCurrentSession().Submit();
4 ]; f, ?; r- I( H( b# m4 YgetCurrentSession().RollBack();
) S# `" B3 z, h( J9 p4 ~...4 Z+ o, |* d! W# R
Unbind(HttpContext.Current, SessionFactory)
1 q1 f; Q7 L f+ K* R2 T8 }
3 l* |; S+ L) }, n M4 M tcontext.EndRequest += CloseSession;7 U' r8 g6 ?& d, y
1 P/ ~) e$ ]2 l7 g$ A
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|