 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
1 j. f2 g4 t' j( {# q8 c2 W基本参数:6 c4 B% y* {. x" s
jta | thread | managed | custom.Class& P0 t% S1 d4 x3 r& G9 F; y4 ]
主要是配合4 k( I; |3 |) L9 a8 B3 S( q
SessionFactory.getCurrentSession()6 Q9 K7 c9 X" [4 W
来用的.
5 `+ C! B& D7 yThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 d2 P7 ~! F% Q) J% I
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:; }; R% h# ^" n% m6 \5 C
context.BeginRequest += OpenSession;
( j6 p" _+ r9 h, F, V+ x.Bind(HttpContext.Current, SessionFactory.OpenSession())$ D$ V/ r: F0 [ ^% B/ J
% z6 _' ?* L2 u+ x//do transaction
2 M7 X a7 L+ C; I: MgetCurrentSession().Submit();: O: f; L2 h! g8 O; h+ w
getCurrentSession().RollBack();/ C7 {6 v; F1 ?; y, k. q- w x' p, y* H
...
- X" y: g" Z* P& N9 F- FUnbind(HttpContext.Current, SessionFactory)
6 H) w/ i% A/ W+ O$ M- W7 K: z# {% w8 X5 Y
context.EndRequest += CloseSession;
( F, {/ m, ^7 }' s; y2 @, W0 h0 H8 c7 c
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|