 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what( H9 \+ L: e1 Y6 B! k5 `. }' I
基本参数:
* f4 z& @9 l9 zjta | thread | managed | custom.Class/ H% Z8 E4 z1 _4 `
主要是配合 m: W) t& k5 Q/ J T5 Z( V
SessionFactory.getCurrentSession(): ~8 M; C$ \/ e- N8 M
来用的.. U0 [$ ]& ?& ?. l' u- w6 O& Y
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,/ n, P+ B( F- I2 f. ?
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:+ X& P( j" E# x' v; S
context.BeginRequest += OpenSession;1 }. E0 r3 G5 n3 {# @' a
.Bind(HttpContext.Current, SessionFactory.OpenSession())+ V4 B5 N1 @' E6 }5 t. i; W
3 s; U e% [! k//do transaction
3 J1 P4 m k6 E9 d, _7 ] NgetCurrentSession().Submit();: H% c2 S! O& N
getCurrentSession().RollBack();
* @4 C6 b7 \$ B3 P" R8 X* O...
7 M2 }0 I) J/ _6 V+ b pUnbind(HttpContext.Current, SessionFactory)' E% O) I2 N( s
5 D8 }/ s5 H4 j* R# hcontext.EndRequest += CloseSession;
: h# ~3 O! {3 c. t. q
- W' c+ k' v q, l, `: z 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|