 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
4 y; {3 B- `* l! c* e基本参数:
F* j& y( R3 M7 J3 K8 b! Bjta | thread | managed | custom.Class2 k% J0 j$ \5 ?1 Y! G: Q& J+ G
主要是配合
. I3 h, h3 t% HSessionFactory.getCurrentSession()3 N6 R: V' l* M% b* J+ S% T
来用的.
' o( X& P; M: D4 W. ?3 fThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
/ x3 k" R v6 k1 t2 ~7 ?8 YWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:' Z4 w1 o' i- e) K: s2 l
context.BeginRequest += OpenSession;
9 r3 ]2 G8 S3 B- _! ^.Bind(HttpContext.Current, SessionFactory.OpenSession())4 \ |9 L1 k& j8 G8 Y, e
7 S8 Z, r( w; s. z9 S, R: O. X, W//do transaction: i( ~7 C2 o! V, }* y
getCurrentSession().Submit();
8 C$ }! \5 O- ?" N0 n0 V. UgetCurrentSession().RollBack();
8 p7 f' j3 I- W l...: U3 X5 r5 x% {% L2 e4 H: K
Unbind(HttpContext.Current, SessionFactory)/ X/ z P4 f: y* x5 G- ^( R
$ A5 u0 k/ h7 S! r, U6 Z/ ?- Fcontext.EndRequest += CloseSession; p/ V, H. Z# w0 F- _$ k- S; K! ^
$ M: J* N8 u3 P, I7 u- t' \
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|