 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
7 f9 Q3 s3 s2 Q' c$ U0 S# z' q& o基本参数:
, c' Q, I2 F6 D& p: qjta | thread | managed | custom.Class
& g! j0 X( j u% U: |) e/ m. r0 W主要是配合' l7 W$ h8 ^* n* o( }% Z1 z0 Q! @7 e
SessionFactory.getCurrentSession()
' K# ]( z# y" g# o, u来用的.
& w# N b6 ]- `5 q# Q4 u' V [Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
7 A& r0 k9 n' N7 t+ i2 cWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
( p+ H& Q0 ?9 ucontext.BeginRequest += OpenSession;
* P1 U% A* P/ D; f) v) H.Bind(HttpContext.Current, SessionFactory.OpenSession())
; V+ K s |- w3 G4 T2 ]; h1 ]" p' K) o C
//do transaction
4 B) R! b8 F' D" ygetCurrentSession().Submit();
6 {1 L: ]% N" Y4 C3 U5 D0 ?getCurrentSession().RollBack();2 |$ @+ O; r8 Q1 Y2 ~, x
...
4 a' y0 n1 K6 E. P6 X8 Y/ \' ?Unbind(HttpContext.Current, SessionFactory). C$ K/ b' U5 W1 p, {4 ^
2 ^1 j4 [; w7 _" C, e2 U4 M& K$ \- xcontext.EndRequest += CloseSession;
) S4 l Q+ H' Y/ B) x$ `% A ^( f3 _3 y1 s0 p, D1 k' v2 d/ Q- S8 K
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|