 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
6 A9 _" a4 P5 M6 m( Q) w0 J* q基本参数:
6 R! x% \: N5 t$ r$ }* @% bjta | thread | managed | custom.Class
! E& S" C* O3 C! h0 y2 A. ~主要是配合3 m5 T5 o0 B, c/ q
SessionFactory.getCurrentSession()
! ~; F. S2 A! l" j, ~) ? S来用的.) Z4 F( ^' S( S
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,1 P) ?. n W3 Z. G* |
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
) @$ n v+ _: b% F( b; l8 _" @' p2 x3 Zcontext.BeginRequest += OpenSession;
# w7 N1 D% G3 J0 q+ Q.Bind(HttpContext.Current, SessionFactory.OpenSession())
; S- V- g3 J; e7 t3 n
) [6 _" A9 |' c7 L4 e4 t5 c+ \2 r2 f//do transaction9 @7 ~2 K p% U, N
getCurrentSession().Submit();+ S* e, } l D7 m
getCurrentSession().RollBack();1 a5 R0 I* C7 X' i8 }1 m+ p. a
...
) ~( V, I- b. ]0 YUnbind(HttpContext.Current, SessionFactory)% n6 t( J/ [4 O: W* M F8 ?% k3 t3 N
' E, @* Y- y, _: j, e
context.EndRequest += CloseSession;
0 X, _; h* W" d6 F5 J/ P: B* ?9 F% I7 b1 \% ?4 w8 @3 Y/ v
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|