 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what8 P$ ~" ]5 y. Y1 Y2 U \
基本参数:
4 v3 U+ k; j i4 b5 e i7 ]jta | thread | managed | custom.Class- l+ [7 o* r" ]' d& u9 U0 S; R
主要是配合
. t$ T1 [- K9 d `5 f+ {SessionFactory.getCurrentSession()
3 S2 W, y. t& c来用的.
* r, p& _. N# v+ PThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% P: y$ }8 e, `9 c7 S1 b& V
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如: ] E6 Y- T/ ?% M8 C+ s
context.BeginRequest += OpenSession;; Z" l& F/ B2 G" g
.Bind(HttpContext.Current, SessionFactory.OpenSession()). G4 b% L5 I/ a B. [. l
% r( [5 B6 v4 q* K( b. u
//do transaction) c* Y& k, ?* y# w5 S
getCurrentSession().Submit();
4 W) I4 [ J: M, YgetCurrentSession().RollBack();
, y& A) N5 [ g0 f5 U...$ W9 T8 L" v3 q7 S) z) ?
Unbind(HttpContext.Current, SessionFactory)
) l* Z" R- ^! M9 [# _. u- u7 ~1 i1 ^ F! [% ~! ?+ q
context.EndRequest += CloseSession;
' o w+ U) b, l1 I% P3 _" p& k9 A3 R) R
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|