 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what; c2 r% Q9 o! _, k3 h. g% ` v
基本参数:) I, ?3 p5 {7 P. S, T1 e
jta | thread | managed | custom.Class* x2 k7 m# X$ R: K5 D$ F
主要是配合
, K3 V5 B" n/ g9 vSessionFactory.getCurrentSession()
5 S: l4 l( Y1 p3 i! v, N来用的.5 i3 T. H. w) P( X" F1 j J
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,1 d% L5 T" } ?4 ?3 j% V
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
W! |+ f0 M2 r' p3 Hcontext.BeginRequest += OpenSession;
- T' z- C5 o1 F; _ z/ }4 u& N. `.Bind(HttpContext.Current, SessionFactory.OpenSession())" m9 M' o1 C1 T* B$ T: r
! k6 R# d& N) D0 X! Y% T
//do transaction
) k# _9 j; e0 {/ q( G* f- GgetCurrentSession().Submit();: h, E7 d. \- e( b5 X
getCurrentSession().RollBack();
! N9 L. N- [/ ^...+ \1 }* v! Z) R+ ~; C: i+ J
Unbind(HttpContext.Current, SessionFactory)# Q* x% J- X% c& \
0 r. C5 u+ ?( Pcontext.EndRequest += CloseSession;
9 q8 M# x* P- }; v5 h* `$ k% Y
& C2 a3 ^& i) v4 q4 w5 J 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|