 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
" Z" i- C$ O+ p基本参数:
! `: T. D; r- Q9 F6 f# \' u. Djta | thread | managed | custom.Class
2 l% Z0 u- \6 g, G& m主要是配合6 I8 W& W F' o$ n
SessionFactory.getCurrentSession(); A; b2 S/ R1 C1 i
来用的.
+ ]: Y$ u/ Z8 {& q. @4 oThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
6 h& G8 P9 [2 B8 D3 q* [Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:& l. k' U# Z- O5 I+ D \
context.BeginRequest += OpenSession;
K# o1 j+ S' e. [2 f, J) V.Bind(HttpContext.Current, SessionFactory.OpenSession())
6 S( i/ O/ s L @' e
+ }+ t' ?& |5 G3 y5 ]. J//do transaction
# j; T0 V9 {$ z7 D) kgetCurrentSession().Submit();1 F8 k' j6 D/ j J% E; f
getCurrentSession().RollBack();
) ?# _3 r- N5 h...
1 e% ~8 A- [/ F, r n; b3 u+ fUnbind(HttpContext.Current, SessionFactory)
4 `: `! ~8 B; J2 x, A! I( Z+ z- ?2 H( [
context.EndRequest += CloseSession;
. J3 i$ L6 Z% Z
( C: i( C* B7 [+ H5 } 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|