 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what) h; \5 @( \& f9 q) I
基本参数:
: i, ~1 {1 m$ m& c4 U1 i- _jta | thread | managed | custom.Class
- s2 a) f4 P7 f4 U( o% Z5 Y主要是配合
- l& a2 J% H4 U$ G" sSessionFactory.getCurrentSession()
) U* b/ H" y& l& |; Z来用的.
5 D9 G( p, t, A( Z) A0 a% t- q) UThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
8 i# f5 b1 \2 C/ dWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:1 i3 S+ I7 o( E
context.BeginRequest += OpenSession;
) Y7 @( }+ @; }8 Y.Bind(HttpContext.Current, SessionFactory.OpenSession())
/ o" w' d# }1 n e/ [% t' A6 g+ Q6 I% O }- U. h
//do transaction9 F# Y* I5 F+ {5 I# K
getCurrentSession().Submit();7 [1 }' C! C7 t0 `% N( Q/ j
getCurrentSession().RollBack();
* V7 }( B1 B5 j) V& ]9 d...6 Y; n- F5 z: d
Unbind(HttpContext.Current, SessionFactory)% X$ M l B0 n8 n4 I
' m) ^5 L5 |' ]8 xcontext.EndRequest += CloseSession;
. u, j6 }! C i: M, y
4 X s( w+ D6 F, U 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|