 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what6 s7 H2 Z3 }1 u7 g7 Q9 C
基本参数:
, C, c: b1 A5 xjta | thread | managed | custom.Class" {8 t! D1 i8 K# h" e; P- k" _
主要是配合: J5 w# G8 R, G4 g$ E, S( H
SessionFactory.getCurrentSession()9 X; s- d$ ]: X1 f6 l
来用的., L7 V+ o b% E) b6 Z
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,. T J- B, z; }5 |% I. }
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:, F6 q, Z+ D3 g- G& `
context.BeginRequest += OpenSession;
6 ?+ f9 |( U2 j% E4 A) v.Bind(HttpContext.Current, SessionFactory.OpenSession()), C K3 _2 n2 W {& Y6 o/ g2 |( e" F
) K% u V" E7 P4 K" \
//do transaction
, j! @7 F4 Q0 r) r5 C# t" }getCurrentSession().Submit();3 z- b3 K5 E. P: z$ `* W0 H
getCurrentSession().RollBack();
" S% v- v0 S& p4 S( X, k1 J2 N8 Q...
; [ t( u+ C5 C A3 @0 _! Z8 XUnbind(HttpContext.Current, SessionFactory)
3 U0 h* U- r7 m' y. U1 P; o. h2 K6 L: U! B) L
context.EndRequest += CloseSession;
7 K" j! g: [& ]3 _$ Q# K) ?% Q
' X& Q7 c/ _+ b 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|