 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what. V' P3 X: V7 i
基本参数:
# Q S& v' [, i% z9 l& kjta | thread | managed | custom.Class
$ I% Y3 d# I% |5 C" X) j) p7 h, |主要是配合
* D8 R6 p2 G6 w6 x E8 V0 ~. Y, _, i7 pSessionFactory.getCurrentSession()" f0 ~6 {7 d1 ~- a+ p3 s
来用的.( u* s9 _) v+ k( E% K
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,6 u! d6 J) h# M
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:% t# Q: C1 w% h! Z5 k5 j
context.BeginRequest += OpenSession;
! N& K5 `/ {7 l ?+ w' E6 o: P/ B.Bind(HttpContext.Current, SessionFactory.OpenSession())5 v8 p8 E, M5 e1 I' K T
. G/ B2 U- G0 c8 h, I- j
//do transaction
4 I7 L& n; ~9 M) b# o9 _getCurrentSession().Submit();
4 h6 k! D9 h- f$ O* bgetCurrentSession().RollBack();
5 a' A7 Q* a, P7 k& G...6 k6 g) o7 h1 z% ~9 T
Unbind(HttpContext.Current, SessionFactory)' E6 m8 W8 p' w
6 Z1 j4 q0 Y2 x- ]" i
context.EndRequest += CloseSession;
% `' Q' F5 J$ I% F" {$ h7 S( K* S2 U5 t
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|