 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what8 G5 W5 Y& S* I8 w) q
基本参数:: i- r! K( [* f6 w# A- p
jta | thread | managed | custom.Class
, X2 W" |0 i+ |主要是配合
$ R5 m! j0 z" v$ v6 ?7 mSessionFactory.getCurrentSession()
; q) h: L! ^0 q+ N3 s. M2 g来用的.
/ ~2 E, N" M4 F7 h& j/ \Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
+ p4 H, A- `) s% S V: lWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:/ C- k* `# u5 ?* Z1 o7 c
context.BeginRequest += OpenSession;0 |& y S+ F3 ]/ H" a
.Bind(HttpContext.Current, SessionFactory.OpenSession()), N; [$ T! k9 _
5 B/ k( q9 U! A9 R; i
//do transaction
: f* z( I1 r5 I9 b3 zgetCurrentSession().Submit();+ k2 m# m: i& _, _! M* `
getCurrentSession().RollBack();: ]" [: P: S* g$ f
...# z% }1 G! c5 E7 y
Unbind(HttpContext.Current, SessionFactory)' Y' A; N' }$ Q" a3 F
. s- x6 u; f8 J4 v& g2 M
context.EndRequest += CloseSession;% e! y1 h; l: s2 w& f
9 j7 L! ^9 L% n3 P9 D3 _3 a" d
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|