 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what: {3 S- ?8 D% N" T- T" j8 k0 T n
基本参数:
* ]4 Y) A9 u& x9 y- P$ rjta | thread | managed | custom.Class
8 w) ~7 g# }' u( ~主要是配合% k3 S' g: s; ~( _! C
SessionFactory.getCurrentSession()
' r' _% W- g. b$ P4 [$ A来用的." p# I6 ?2 X2 }
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
$ \# P# r) i( RWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:2 J2 Z1 b1 ^) o' u; m3 \) o* }% v9 E
context.BeginRequest += OpenSession;5 f( G" `1 E4 h7 h1 R
.Bind(HttpContext.Current, SessionFactory.OpenSession())& A. L: V: N1 J- d
. }5 h; @3 q0 s7 {, t* J//do transaction
# N# I" j. B9 g8 U4 A/ V* g" ]getCurrentSession().Submit();
2 F3 f/ L5 E k2 ZgetCurrentSession().RollBack();
$ p- S$ H" _2 E9 J' W.../ C& H8 C) ~& O, d1 W
Unbind(HttpContext.Current, SessionFactory)
( ]! X* q' a) t' a% s. b: S; q, T0 u7 y. }
context.EndRequest += CloseSession;( {8 p$ I) }" T1 S$ d
) n4 q. N$ W3 g [1 e% I% {
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|