 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
- w5 F( |: b- C7 [; V' z7 X基本参数:0 K( s5 Z8 @( A, e s
jta | thread | managed | custom.Class( s6 h% m& u8 t0 O |. y) H
主要是配合$ D; U7 P8 E0 d* Q( U/ }
SessionFactory.getCurrentSession()
9 l+ r) v& K5 ?( U来用的.6 {4 `" ]0 @5 L) `' |
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
4 [. w$ f {" c9 W3 [) A4 pWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:. q2 M- k5 W m+ t) D2 k
context.BeginRequest += OpenSession;# b+ \1 @% J/ E3 D
.Bind(HttpContext.Current, SessionFactory.OpenSession())
; L7 F% @6 Q; L0 m
! p% r2 D& \, `7 U//do transaction# S& m0 y, _% D5 A) p
getCurrentSession().Submit();4 Z% c$ P) v( @8 m
getCurrentSession().RollBack();
0 D6 y$ W" [& q9 C. m' L...5 y* L( @, Y K; P0 ?6 ]: t
Unbind(HttpContext.Current, SessionFactory)
4 q2 `2 [, c2 @
& b" j5 c; g) M0 w0 P2 Ucontext.EndRequest += CloseSession;" Y2 u6 |0 ?* U) o
1 f/ n% W; Y2 O; O7 a 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|