 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what; K. d. T+ \' k
基本参数:
: v$ t/ b" G# F B) W! ]2 ijta | thread | managed | custom.Class4 ~& P. p0 R. P1 V' `4 U! G* F
主要是配合- d! x0 W7 K- c* J- h8 P- P
SessionFactory.getCurrentSession()
F |/ y7 q# W1 S( I; b' Y$ n来用的.. x/ V- | w1 H) z1 d/ U; c
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,: }# k& V5 O6 |0 v; i) I( L3 u) x
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
+ d/ p: Z/ D" `# y( G' y, Econtext.BeginRequest += OpenSession;
N% l$ r; I. g+ d.Bind(HttpContext.Current, SessionFactory.OpenSession())
1 x' L0 Q p$ V1 _& r( p' g5 k# h0 m+ _0 {% p4 x- Q! g
//do transaction1 g) t8 x+ k: x" d( p+ W' M
getCurrentSession().Submit();, i Q0 N, ^7 z5 C. Y
getCurrentSession().RollBack();
2 _. p' A- m5 j2 H...
7 r8 J3 {) ~; h! e) u1 P; C/ uUnbind(HttpContext.Current, SessionFactory)
! U, d# h/ u! x. o1 ?/ ]6 C) U3 D- U6 c. @& `7 C
context.EndRequest += CloseSession;
! l9 {/ B3 j$ W4 Z3 L" C/ H( ^3 o, {1 H
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|