 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what9 N% `% v4 k% M- G2 f
基本参数:
* R2 o! w- r" v! g$ X5 x' d w* m3 @jta | thread | managed | custom.Class6 T1 t- j9 }, H* O6 g
主要是配合
1 u; l' p# X: j+ i8 n. ^SessionFactory.getCurrentSession()
* l& N3 c( d3 P; |) \来用的.+ c) E! `; R& B* ^# `' T
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
6 _: ~* a3 g, u& NWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:' w/ R1 z" \& _' e. t1 j
context.BeginRequest += OpenSession;4 w+ i: ]! ?$ y5 B
.Bind(HttpContext.Current, SessionFactory.OpenSession())
3 |7 o/ a% t; m7 c# A1 H. ~) Y6 `7 f" o+ @( D- E0 p% ?6 V
//do transaction
1 T+ m1 k5 l& Q, H7 c8 |" [getCurrentSession().Submit();
( J7 c' m( c C. u" y$ r$ V( F |getCurrentSession().RollBack();, F: H! B6 k, R0 x/ n2 Q
...
' f9 @1 Z' @7 V0 F5 KUnbind(HttpContext.Current, SessionFactory), r) e3 i9 x& s2 j$ p3 r+ K1 J
! v' H5 b3 I0 L" n& z6 _
context.EndRequest += CloseSession;
. S/ F0 X& C3 B% V* ^2 R; h; Z; P$ u& @) u8 w
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|