 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what1 d: @/ T) P7 t3 `) R# B1 F
基本参数:
& N- w9 J0 `/ r* S$ z' }2 u6 Rjta | thread | managed | custom.Class+ Y' j$ e4 |5 f- O1 }* I% ?3 B
主要是配合1 U( H1 e( ]! ~4 Z2 X8 e
SessionFactory.getCurrentSession()! g% {7 u+ i: m J3 n) _- m
来用的.
- r& G9 I6 ~4 |+ q: ~( |( VThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,' J5 g$ v2 g, W6 [
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:7 R8 g4 ^9 f# z
context.BeginRequest += OpenSession;8 |0 p9 |! N$ H& U
.Bind(HttpContext.Current, SessionFactory.OpenSession())) i( n& o3 j6 a9 }$ n% }3 u
7 N( B) L8 }7 P0 v" `7 d//do transaction8 j2 F; Q8 }, V% f. l$ |" E1 Z
getCurrentSession().Submit();! ]* {' e& H2 ^ w \( l; g/ Z
getCurrentSession().RollBack();
3 d0 S4 e. F0 U0 w6 y) T) r1 t& |% ~...
5 `/ m8 s% h- f$ L }Unbind(HttpContext.Current, SessionFactory)
: ]( p% N" l; _9 A# V. _; w* R' e& x' A' R: a4 q0 |
context.EndRequest += CloseSession;
' p, \3 C4 y0 o7 C l& N) m
; E: B' Y: y" k 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|