 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
9 U& b4 U! l; k基本参数:
0 @, Y* t/ \( j+ j6 X0 jjta | thread | managed | custom.Class
6 y: s% Z# Q* o" F主要是配合- k; Z6 G! \; y+ x
SessionFactory.getCurrentSession()
! W. z5 T4 c; I2 Q2 a' I2 G来用的.
3 n+ I U0 J5 `+ fThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
) Z1 v. w/ o2 v) q! k0 LWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
4 D! a9 S% S: ], k# T# z, l4 p( Lcontext.BeginRequest += OpenSession; H0 K2 ~9 v* n1 I. H+ ~
.Bind(HttpContext.Current, SessionFactory.OpenSession())8 G' M% }7 o5 X8 m# [# P0 B
4 r x- n: E9 o, w! ]; }
//do transaction
3 T o& n& x- w% l4 SgetCurrentSession().Submit();
y- _" O: o6 F. b* e, UgetCurrentSession().RollBack();* i3 w* E9 p1 N8 U6 m
...
5 K3 N1 d+ m% M7 \& RUnbind(HttpContext.Current, SessionFactory): S1 I. T8 u$ e% j
U* s, ]: G1 } j% K& [context.EndRequest += CloseSession;% M; `# G$ v% t- I2 s! g
0 {4 g% l9 } e" r$ W$ x/ I
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|