 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what0 O, |' L7 S1 D3 _0 U# ~6 T# C
基本参数:, x% I& s4 \! d# V# {
jta | thread | managed | custom.Class
# d6 ?+ }- }4 {6 j- y2 E* `3 i主要是配合: Y- B, x7 V9 o6 w
SessionFactory.getCurrentSession()+ g' ?; O# D* U/ X' ^6 `
来用的.
6 n9 \2 s7 _- gThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
+ K/ h2 J: P% T! R: N5 G1 H |Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
* V9 _3 v2 n! O! O+ Mcontext.BeginRequest += OpenSession;2 B; O$ W( Q+ h( T6 @
.Bind(HttpContext.Current, SessionFactory.OpenSession())5 k; Q# f$ G4 f% e% h6 d9 k2 R
! V4 C2 ~' e f
//do transaction; \8 ?# [5 O4 ]) h; ]
getCurrentSession().Submit();, S8 J7 ~, k4 f5 k7 B/ Z
getCurrentSession().RollBack();
" @# n9 [2 l, _6 ^ ] U) Y...
' _9 }* _: P7 i) C5 ~4 X7 G' cUnbind(HttpContext.Current, SessionFactory)1 x3 k5 I: g5 ~ l( Z4 u0 v
1 m+ o6 X% [, jcontext.EndRequest += CloseSession;
5 J ]( L5 \) p6 y) A9 l6 s8 e( ]) c% _' n/ q- L6 F7 }' A
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|