 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
, H; Z9 {- H' |4 I) G& S, f9 Z6 _基本参数:6 ^6 }1 A8 s* K3 `1 f3 K
jta | thread | managed | custom.Class' q' u+ v4 L3 k0 D' j
主要是配合- g% ~% ~6 u! d: {
SessionFactory.getCurrentSession()
( T6 l) o7 k: Z& a* s来用的.9 ^; M% r1 |- s( P" }7 c* a
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 N, ?) R6 Z' z) N
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
) T! m0 M/ a6 O3 A2 k* v4 scontext.BeginRequest += OpenSession;
. z* f, G4 }! L8 {+ h: T.Bind(HttpContext.Current, SessionFactory.OpenSession()) O r; {: n6 c
, H3 K; S) X- X: X) W8 Y
//do transaction9 r! d$ M% s0 V
getCurrentSession().Submit();9 H- J' v" z- C n) J
getCurrentSession().RollBack();* w9 M [; |4 J5 k% o8 T: S. E
...2 v0 z1 [2 s0 n, M9 `3 y
Unbind(HttpContext.Current, SessionFactory)
' H+ _6 h4 p, A% H( Z6 y4 l4 }: h) s+ g" m$ G* P
context.EndRequest += CloseSession;
3 t g/ T) k8 O' Y% ^. X S) u* ~' V9 W$ F) A( L+ I% T
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|