 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
& \8 A; e2 ^+ n基本参数:% P: P; n- V( K/ h! R T- Z
jta | thread | managed | custom.Class7 `1 V6 a, a3 b/ }( B# v
主要是配合& Y6 R: g) [" a
SessionFactory.getCurrentSession()
# P6 x" N8 p. \# z' x" g; ]来用的.6 o! h" g9 N2 N8 q! L) U1 @
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
0 P" h# o- }8 }/ jWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
$ L+ s m6 P) {3 a' |( B& Q# K3 Z9 Fcontext.BeginRequest += OpenSession;
5 }8 f# h; Q+ C7 W% }4 I8 O.Bind(HttpContext.Current, SessionFactory.OpenSession())
' m3 O; H8 j: I7 C* N$ W; d0 j9 _/ H# O, A) o8 R3 f
//do transaction
; }" z4 |9 c: ?4 h0 x3 r, ?getCurrentSession().Submit();
4 D* N8 ^5 T) c( h- @! pgetCurrentSession().RollBack();: b+ l' W/ Z/ N/ T% x) f$ d
... U; f1 i6 K/ Z$ t% g) X
Unbind(HttpContext.Current, SessionFactory)
. K3 S! Q' Z% L4 f5 a; |. y o( v: B
context.EndRequest += CloseSession;
4 O$ `. b7 d, [& d z8 V$ K- G* M' t3 V# U
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|