 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what8 d: @; k# E6 B$ z8 _. M7 n1 B
基本参数:
, Y6 K' D( j4 m& [! P3 i( Qjta | thread | managed | custom.Class
- f1 V% L5 r0 R主要是配合; z" a, a3 q# w/ i- l
SessionFactory.getCurrentSession()
( q. t. {! @) h3 ~来用的.$ k# o6 q2 {3 i7 } Q
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,1 Q! Q! O; D0 }
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
; U) o- c" }( s2 b C( G) Ocontext.BeginRequest += OpenSession;, U. x+ I( A! Y9 {# w$ o! }, \
.Bind(HttpContext.Current, SessionFactory.OpenSession())% l+ X3 _5 J7 u; I m; U
$ V& c2 W1 Z; ]9 h) u0 ~
//do transaction0 b: _& B3 C3 e) U' k, n
getCurrentSession().Submit();
0 O. T1 c5 L7 _0 b) {; ngetCurrentSession().RollBack();3 u& _+ N. [" u* f# y* T' a& D3 R
...5 i- g9 F; p! A3 L* T" z& O
Unbind(HttpContext.Current, SessionFactory)
1 Q6 n0 ~7 Z& v6 j/ c4 c! G0 F k
8 V; s) g+ P' T' J9 ~7 C" qcontext.EndRequest += CloseSession;
) d j' s5 N$ l1 C/ t8 G
- n3 g; [: l( I& E% x 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|