 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
d4 }9 W- y. |) H+ }. \基本参数:
# C5 e% {; Z( J/ t6 E1 Ojta | thread | managed | custom.Class7 S6 y& ?2 v. n$ `- O% W5 E. {
主要是配合( s: Y( R8 B2 R4 A$ H8 J
SessionFactory.getCurrentSession()& @$ x! E0 @. `+ s; t! ]/ U
来用的.
' Q+ ^) U. A* ~: qThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% \: p+ d7 R- \$ G
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:9 K5 z" t7 b3 _; d$ X- O
context.BeginRequest += OpenSession;
0 ] X1 B' G( X; O3 ~.Bind(HttpContext.Current, SessionFactory.OpenSession())
8 P5 _2 ]/ H+ y& P; n- t' Z. R
v6 ?1 u$ a2 B2 y% P* x, l" m//do transaction3 j U- O4 v& Q3 N: {6 H
getCurrentSession().Submit();' }# U; h* i! I6 ^# X
getCurrentSession().RollBack();
% E6 y$ y" L* k. F- W...
: s8 @* o9 i+ NUnbind(HttpContext.Current, SessionFactory)- c$ `# h; q/ Z& U
! i8 M5 @& v1 m8 U! j* j9 [context.EndRequest += CloseSession;
8 k1 V/ `/ u* T" u7 x" \( U1 [
l- h5 o1 U) l5 i1 l 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|