 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
6 b/ A" q6 N! w; w基本参数:
2 q9 ?3 R6 c- |jta | thread | managed | custom.Class
# y* B2 h. o3 J7 D& S主要是配合3 x8 g& {( F; n
SessionFactory.getCurrentSession()
L- G+ k' ~; c9 r; Q! q来用的.% b5 b! v0 M8 Q& \( y1 r$ z( D
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确, P( c2 K! J2 T" F
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:$ r1 O* n; A6 k0 G8 p% Y
context.BeginRequest += OpenSession;
! u$ i$ ?2 C% X.Bind(HttpContext.Current, SessionFactory.OpenSession())+ `: `9 r- D% k, d$ L( T ^
% A, I( o& b5 t- M) _ K//do transaction# Q6 ^) w4 W4 l+ S
getCurrentSession().Submit();& M% K3 H2 v/ | `: @# y
getCurrentSession().RollBack();
0 ?& m( W0 k- |7 a3 s, r...' o' u: _9 Y2 r+ F
Unbind(HttpContext.Current, SessionFactory)
3 _: y. D2 E. I( n
$ n$ b& i g. R: p7 x" G7 Y8 l$ rcontext.EndRequest += CloseSession;
4 \6 n8 E* ^2 E" z7 g T, R+ _5 o
5 P4 E* _/ O% P; n 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|