 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
3 Q3 G ?0 P3 {: l基本参数:( f( h* ^# q& P5 r$ F* A' u
jta | thread | managed | custom.Class
& a/ U. l9 c" t3 [) ?, r& G: b6 F2 K主要是配合9 c) L* _) b5 N( G. K4 A
SessionFactory.getCurrentSession()
+ |! o: d( s! d F. y5 Z: Q来用的.% T6 s" B: \" `% r4 K' u/ Y" V/ ?/ [! ]
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
2 y3 T- b, p T' Y( cWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:* H$ @5 S4 y2 k7 w0 D
context.BeginRequest += OpenSession;8 [7 a* l; I2 g% q( g9 I* f& l$ ]
.Bind(HttpContext.Current, SessionFactory.OpenSession())
1 g: B' y) D( x6 `$ c R* z1 }& B: D2 G7 f8 x! m a4 v( [- i
//do transaction, \8 ?0 N5 |4 e h. Q" q0 Y
getCurrentSession().Submit();" n: u) n1 ]0 `' O- u1 f
getCurrentSession().RollBack();' f( F+ y9 L2 m/ q& R
...
% @9 X R. E7 l6 PUnbind(HttpContext.Current, SessionFactory)
0 P5 f: _; [6 l0 Q# l, ^, o2 |; a2 y1 `! y% `; H
context.EndRequest += CloseSession;
8 L3 ]7 ?0 m; J+ ^/ ?) f- Y9 i
" c: l. ]/ l/ {; l, r 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|