 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
4 U N7 s3 u0 c3 m r基本参数:
1 a5 h& v) m/ b8 A9 U6 ?7 ~9 Jjta | thread | managed | custom.Class* I- ~/ Y0 [: Q3 M1 X
主要是配合
+ s9 {3 E/ V" z4 B/ b! BSessionFactory.getCurrentSession()
9 |9 M+ s' A& x% m" e" ~) u来用的.% z% L. v- [! C" M: b' v S" d
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,, ~' m' y3 n" D0 K) ^" d& O$ X7 H" @
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:+ l0 r$ U# b/ w- o4 c! r A9 h4 T2 f
context.BeginRequest += OpenSession;+ f4 l9 T- u) M! S4 O7 g' L
.Bind(HttpContext.Current, SessionFactory.OpenSession())
4 o9 g& l5 T; K, r2 I
5 O4 r1 @ G( [9 A F//do transaction
* r5 x) Q; u5 x6 m7 y- ngetCurrentSession().Submit();9 }6 w3 J* y1 a& q8 G9 I
getCurrentSession().RollBack();2 {5 }. B7 s( ~/ Q: \# ~8 B# x
...
- j, R7 V' a6 W F4 {" c' \2 W, } }Unbind(HttpContext.Current, SessionFactory)
0 w, L# H6 r! E; j- q0 ?' ^
4 L- T: L$ c2 P) L q2 N2 Dcontext.EndRequest += CloseSession;+ D" [3 x& H$ R4 Z2 _: x, ?/ |6 T
7 z/ J4 ?( @ N" ]
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|