 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
) V4 E4 e3 P) i* \) c! A4 q基本参数:
/ l, P; L i, g1 t" o j* a5 Ojta | thread | managed | custom.Class
% f* h$ a _, r( X主要是配合1 ~: W1 E9 t3 ]
SessionFactory.getCurrentSession()
. t! f4 @/ N% |来用的.
# k( a) U% J. J# pThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
( H( m' R: m2 J w* a' y9 tWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
5 a2 H; G8 n( s) Jcontext.BeginRequest += OpenSession;
1 V, J. R) y+ ?- X. L.Bind(HttpContext.Current, SessionFactory.OpenSession())
8 Z* R0 [6 w0 K. \8 o
/ C0 K9 @* g. {+ N//do transaction
$ k! A# Y3 g+ e( |/ A1 c8 [getCurrentSession().Submit();
4 _, ^2 }. B4 k* T0 X9 o# ggetCurrentSession().RollBack();
' B, l9 ?& v$ H8 S4 r( [... Y% T6 l6 y2 |# O% M+ y0 J7 t
Unbind(HttpContext.Current, SessionFactory)- L) N9 ?2 K2 i; O- @! b
8 S1 S7 [5 k- Tcontext.EndRequest += CloseSession;3 D+ d4 ^. Z7 d6 ?) \ h
; s$ M4 d% z- h7 |1 A9 ]# d1 O 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|