 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what9 [) M# G. G3 a
基本参数:
M; F% U! Z+ D5 Hjta | thread | managed | custom.Class
1 g) _6 H0 K- S! i3 N0 E3 |2 c6 ^. ], A主要是配合
& {9 o X; a' TSessionFactory.getCurrentSession()
( y# ^6 S* U' x, `: U* \# I来用的.* d/ N7 E( i- d$ t% W L
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确," K3 p+ a; O+ ~# @/ \+ z7 d- v
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:0 d; z0 H3 J7 ?$ V2 `5 |" F' k0 ~
context.BeginRequest += OpenSession;
7 i3 W# K, o. L3 G) B' B.Bind(HttpContext.Current, SessionFactory.OpenSession())
5 a- y# b: s& ]5 x8 T1 D: u* e5 b% c& X+ w, `( {
//do transaction
& D/ O- Y6 C9 M% l! R1 zgetCurrentSession().Submit();0 n' {; Q! R1 D5 t' R+ g
getCurrentSession().RollBack();' q* T8 M4 R; I% v
...+ P: {6 V3 \, [3 U! `1 @, S
Unbind(HttpContext.Current, SessionFactory)
4 C. Y+ P9 E- c, k0 x' i9 S. \* K$ Z* }1 T/ }) ~* t" [; i7 W2 N% ?
context.EndRequest += CloseSession;
& }3 M. P, I6 T' ?% Y* P) _7 R% H- s2 G, {/ Y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|