 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
" h7 r3 _# m# l6 G! P基本参数:6 V3 M: q7 ^* n/ M6 {; R1 k
jta | thread | managed | custom.Class- X5 i7 s* `* x) v# j
主要是配合
2 H: X/ i5 _- Y4 n' gSessionFactory.getCurrentSession()1 @$ y$ {! X# y! l+ ]
来用的.
, z3 \& b' ^; N- D" r: mThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确, D$ |2 g P( e1 |; z4 S. X8 ?
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:; R1 C& n) _6 u+ v4 @- }# o* `
context.BeginRequest += OpenSession;
! S/ ?8 G& ^+ B6 F, d/ @% B. b( m.Bind(HttpContext.Current, SessionFactory.OpenSession())$ d! A( k% u7 G# Y
3 U8 E8 d0 }8 h5 W, j
//do transaction
! k! }1 X' x3 R& r' I- B1 t, p% sgetCurrentSession().Submit();
1 n. U. f! F4 QgetCurrentSession().RollBack();4 j- ?# |7 T5 b( a& ]
...9 [- p" @2 x+ I) ^
Unbind(HttpContext.Current, SessionFactory) m& H( g" q$ Q1 U
* T) D3 m% @' C- P
context.EndRequest += CloseSession;/ ]9 V% q; f2 @! a0 [. _$ N/ n7 ?
5 l( x# |2 u6 ^) b) F
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|