 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what; T" F. f) z/ Z" u' A1 p) u
基本参数:
# E9 f1 H! d+ Z8 ojta | thread | managed | custom.Class/ b% t. J& w, ]) c, m" m& C- J
主要是配合
( w) l1 i4 B1 r" b9 E) |7 WSessionFactory.getCurrentSession()3 z4 x/ p: f0 y1 y
来用的.
% t8 ]' s+ A4 O: FThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
; }7 M+ ]( c' E/ k3 m5 xWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
! ^' z' M* }$ mcontext.BeginRequest += OpenSession;1 M0 e7 F$ f- B" h" h8 k" j
.Bind(HttpContext.Current, SessionFactory.OpenSession())8 ~9 g0 _( M: J& g1 A' D
5 N# D5 I! D E//do transaction
3 `- O1 j9 a$ { s2 C, EgetCurrentSession().Submit();
% D7 S. q* F, C" ^9 bgetCurrentSession().RollBack();% q0 \6 p U+ f9 Z: M" |1 J6 W
...
/ h! C: i+ V! E/ Z% |& |8 v, uUnbind(HttpContext.Current, SessionFactory), ~ N$ X: x& |- {( N% v
2 c( _: t v) e, `; s4 {
context.EndRequest += CloseSession;
/ k( I: }. V. G7 \% b4 X+ X2 O8 p/ U2 u3 f9 Y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|