 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
, |; y/ y$ y. E n% y" E/ j基本参数:
: s4 s& P# \* n9 I. \) v+ {% X9 J- ?jta | thread | managed | custom.Class' G( ~# ]/ B' a) q1 s$ ~
主要是配合
( c- m, p9 v3 p- zSessionFactory.getCurrentSession()6 @) Q6 ~! ?- d8 o5 R( G) q9 M
来用的.
2 i4 J; Y3 p8 T" u+ U2 K7 f. q& P- qThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,& B/ }: U. B, k/ A& U1 Y
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
7 x u7 N5 |+ z1 X' E) V4 o0 _context.BeginRequest += OpenSession;
* n' G6 }7 K% w.Bind(HttpContext.Current, SessionFactory.OpenSession())
% _- T1 f& j' A. {, o0 J5 a) j A
//do transaction
3 f1 s5 ~* `2 ~+ o) {6 qgetCurrentSession().Submit();
% C- y; @% _" l2 _' kgetCurrentSession().RollBack();
$ q( P5 c* v/ p# r...
4 G4 _ c+ p3 F. p7 ~7 Q3 UUnbind(HttpContext.Current, SessionFactory)4 U4 E- e& l7 `7 g# a" x; |
* o) J1 n& Z. i- fcontext.EndRequest += CloseSession;% }/ S) G. w2 R
: o9 q7 r' ]6 h8 X: S! D8 m 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|