 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what% ~; N. x' U9 w3 }6 p A0 V
基本参数:- w4 f1 [) }4 ~* @5 D6 G9 v; q) n
jta | thread | managed | custom.Class$ A/ P3 m( d3 u- |+ j
主要是配合2 ?3 {; D% | Z
SessionFactory.getCurrentSession()& H. k) g. [ H" y& X2 V' }( ~
来用的., I7 H& |! a3 q3 X
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,. H& D' A5 F8 _. i. @5 }& o
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
+ `& n. H$ _& k( Icontext.BeginRequest += OpenSession;* n7 V* S4 X+ R- j# B3 `& a
.Bind(HttpContext.Current, SessionFactory.OpenSession())2 O7 n' H8 |3 ~% _0 W3 y
" _8 S8 A/ x" q
//do transaction
g- S7 j( H. \( q9 pgetCurrentSession().Submit();7 P/ k% r. ~& Z$ C+ A0 H! P. o
getCurrentSession().RollBack();" \" v# e4 G' ~! j
...
" V; i& w; ^) k, T0 q* V" ?Unbind(HttpContext.Current, SessionFactory)* O4 r# W$ {0 r) \6 Y4 ` k b8 c
1 q1 Y! O4 O, n1 T- zcontext.EndRequest += CloseSession;* o6 b* c) z7 [
0 I0 a; N: x& _% y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|