 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what3 B( s6 f1 K; ?+ t, d
基本参数:
' v. X- |1 D+ z% R" [- l0 b" |8 xjta | thread | managed | custom.Class6 T/ \/ m( j9 V6 s- q
主要是配合" v. }, z* E( r; K
SessionFactory.getCurrentSession(). s5 E. d/ B& }, S x$ n
来用的.
! X$ @4 E& j1 `# ]" R9 O' y0 XThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,. @" a- o' ?6 k- V, l7 K
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:, B! ^7 F$ G. T
context.BeginRequest += OpenSession;
4 Z, h3 H# [3 }3 j3 M! _.Bind(HttpContext.Current, SessionFactory.OpenSession())
/ A# ^* X+ |# }$ B- z) G# t8 [9 v
: G, \( C3 T( s//do transaction J/ _! R' H+ E0 z
getCurrentSession().Submit();6 R0 ?7 y, A; q1 `& N1 ~
getCurrentSession().RollBack();
) }! k$ f8 H- ~! ]# j...- F8 H: Z- q4 I+ C5 R
Unbind(HttpContext.Current, SessionFactory)8 G, e5 Y5 G1 f' S/ d) n
w/ k5 [ B3 y0 I4 m$ f9 }context.EndRequest += CloseSession;( @& k0 I+ m5 Z
+ F% D7 s( @ d. v7 I9 Q" Z 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|