 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what3 @% j9 P7 b9 }
基本参数:' P3 l% U3 R5 f, `. d
jta | thread | managed | custom.Class; B% l8 C% {" a% ~, `: P3 Q
主要是配合
% e( K" G5 d- @8 c- @SessionFactory.getCurrentSession(), c/ D8 _& u& z$ J& Z- T; @
来用的., K" _- ~" i; J' B1 Y
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
* t9 k$ z( _4 EWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:" v0 S" y/ e2 V$ J/ c8 ]
context.BeginRequest += OpenSession;
0 i- Y" I! F1 X/ w3 F.Bind(HttpContext.Current, SessionFactory.OpenSession())
& \) A5 ~9 w' I! I* F7 ]
( ^1 o% P6 [: l' E//do transaction
0 T* \& L, j/ Y) F' [# m8 [getCurrentSession().Submit();
; z( D5 ]& ]9 [+ V. |getCurrentSession().RollBack();
5 q! _) |+ i4 R... o# M/ R; z% l- L
Unbind(HttpContext.Current, SessionFactory)
. {/ X; q6 r9 s2 D3 z5 j. }: E1 Z `7 k5 b
context.EndRequest += CloseSession;& l0 }$ r% o. f; [: p! g
6 A3 i3 b: @3 F5 _, D6 N' w& x' `7 Z
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|