 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what& J! X; T5 M5 ~5 [
基本参数:
5 U3 _" r% j5 yjta | thread | managed | custom.Class Z* {6 X4 Q3 \& e& i0 u8 n2 H2 @
主要是配合- \. V! I$ J6 H, q2 w8 l* }1 ?
SessionFactory.getCurrentSession()7 `1 K& o+ }! d& b! w
来用的.& B) T* f+ L& T* Z, q7 S; K
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
2 I9 L. v! m0 v+ W0 w, P# s1 yWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:) Q% |- e+ D! _0 h9 X
context.BeginRequest += OpenSession;+ M( J* S6 G! V* N. a& n; ^1 v& V
.Bind(HttpContext.Current, SessionFactory.OpenSession())
8 l7 o; [1 z" B3 m. h: Q1 L
! D1 O- y3 s$ ]8 R% M: n//do transaction2 h) u8 d! ~4 h- q/ Z7 H
getCurrentSession().Submit();$ j7 t5 R; ~' r- s) S, u
getCurrentSession().RollBack();
K9 ~: A+ [/ U5 [...
4 c# j' F* ?0 ~1 D, I; oUnbind(HttpContext.Current, SessionFactory)
6 F' L7 }; H) [% p4 a* V; c$ c7 {+ K/ e% P! d$ |" N
context.EndRequest += CloseSession;
/ ?7 a/ t: s% g! ^6 t% y7 P0 v& f* l, O- r6 V/ C( J4 i, { ]
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|