 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
1 V5 r. K- ~/ s2 y. ]1 M) I$ y9 i基本参数:
1 q9 T" |7 {# K! zjta | thread | managed | custom.Class
3 F! ~5 O0 z6 g ~1 F主要是配合7 a% M/ q& E2 k3 o
SessionFactory.getCurrentSession(), P1 r5 F* z# y0 p7 ?% ?+ H
来用的.: Z- g' H- E3 M2 k
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,- o: X% k; \% k- o! o1 w
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
5 x* q/ L) d" Z3 f Vcontext.BeginRequest += OpenSession;
8 Y9 E" t/ I. Q.Bind(HttpContext.Current, SessionFactory.OpenSession())
6 b2 D! I8 ]7 q0 N+ w& u6 c' g
U9 D6 e" e6 R6 w) d. Q//do transaction
0 e# L8 l( M. @( e; ~getCurrentSession().Submit();
% E) V" N$ J, y/ Z# IgetCurrentSession().RollBack();
7 @1 h; n) q$ Y4 {: f( J# v& a3 K( v...7 \6 v8 [2 y' i' A& P% |9 S
Unbind(HttpContext.Current, SessionFactory); E: }" [( l6 {, |% Y
! A1 v5 Q: z) y# V) e4 b/ Qcontext.EndRequest += CloseSession;
( |8 H& f; g4 p6 l/ T( M1 ~7 C/ }( g j8 M- `$ q# ]
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|