 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what t% G& [2 f9 j* g3 L
基本参数:7 X h1 \& Z& C" @' L: g \9 D
jta | thread | managed | custom.Class
0 M: |' D S/ u# }$ I主要是配合- P" S1 \, l# A( ]( Y
SessionFactory.getCurrentSession()
' h- Y: }8 |% d1 y" p7 F来用的.2 K2 B. k, }( u6 h/ g/ q+ Z
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,6 {, g& N7 |& q8 H6 p% p( @
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
; u; |' x! \. O: Z' A; [' ]context.BeginRequest += OpenSession;
- L$ H5 a% z; O+ q3 M- {; r0 u.Bind(HttpContext.Current, SessionFactory.OpenSession())
1 I, V" \+ b% D
" t% t+ X& z; S! z* `2 }4 {//do transaction- [- i, C; \5 e9 r7 l3 u. }+ k& O
getCurrentSession().Submit();! i, ^& G+ i, K+ z7 S/ D
getCurrentSession().RollBack();3 _% y4 x# x/ w% w# B+ M
.... a7 G8 w7 k1 j8 F3 c# j
Unbind(HttpContext.Current, SessionFactory); m& T7 ~/ X' @( q0 x1 Q: Y
% A& G. t- i: H! [* P/ Fcontext.EndRequest += CloseSession;
4 E& u/ X/ g/ I4 K+ h) v" p1 b' L8 Z' y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|