 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what. T$ U& Q1 P4 s0 g1 y
基本参数:
0 o& E4 `# T: v0 Y. Fjta | thread | managed | custom.Class
2 x/ O6 w3 [3 w6 ?1 }主要是配合
) T. y" ]! X. d8 C9 ?/ }; J3 GSessionFactory.getCurrentSession()
. C2 G5 ?9 \1 L' A: r来用的.
' n+ i4 ]4 o0 Y7 FThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
7 }7 l7 w! E( tWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
9 P) [0 F& I! Xcontext.BeginRequest += OpenSession;
. U1 K5 i$ I: F1 J$ h4 T.Bind(HttpContext.Current, SessionFactory.OpenSession())6 G: D) b2 H$ S& F* H/ Y! R! p' A
! l: V7 n& v% J R! Y! ]# N1 L+ Q/ \
//do transaction! @, A1 p Q9 i0 M* P
getCurrentSession().Submit();4 |$ j8 N( C! f F+ z: {
getCurrentSession().RollBack();9 X* p) U9 D3 n8 h. e& H
...
r1 `3 G7 O, Y, s/ e. zUnbind(HttpContext.Current, SessionFactory)
! [ A u8 }' D* L' J8 ]' s4 P4 t; U" X: k' ]! M
context.EndRequest += CloseSession;' ]( t- e4 }; i8 j
0 e" f2 K5 w: F8 @, |2 c
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|