 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what# W- @0 L! d0 t' Y: D3 f: L+ ?3 I
基本参数:
5 ]( n" I. [- r7 `2 F+ @5 q* fjta | thread | managed | custom.Class3 L/ W# F5 Z# ?% W
主要是配合
2 u" q' n% Z' A5 `4 _; V! bSessionFactory.getCurrentSession()7 Z5 Z0 z( p6 c7 E
来用的.' m1 m% _' o5 k- W; k; Z
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,0 e0 N% M G( v) w5 a* ?& m
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
4 H% y! v5 B( t0 ~) h& r0 y( Ycontext.BeginRequest += OpenSession;
3 M6 m& y7 W5 v: u5 V7 w( m8 i1 w.Bind(HttpContext.Current, SessionFactory.OpenSession())
4 |0 a- j) \* r$ |# N* \6 J4 O' ]2 E
//do transaction X5 j: a' A: G
getCurrentSession().Submit();) D8 E* |8 R) b
getCurrentSession().RollBack();
; }! d% C2 Z; s; L" o9 w4 i..., S& C. S& ^4 `4 Q3 |* Z7 m
Unbind(HttpContext.Current, SessionFactory)+ R' V" \: H7 G6 V G
, b& W$ {# w) j- ^4 jcontext.EndRequest += CloseSession;0 ?6 m6 |7 }6 }, q; L# q0 I
% ]2 ~5 h0 s- A2 `) } 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|