 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what7 m) V1 L& I5 x- Q
基本参数:# Y/ J( d+ y i! X* Q: ^
jta | thread | managed | custom.Class
2 w' Y# p* ?# C' M9 M0 t& s主要是配合& } K/ q8 g! i" b" z0 [+ i" a+ E" y
SessionFactory.getCurrentSession()# N8 k$ d t* J6 e; P- W# U
来用的.
, Z0 n; Z# D$ t! c+ f# r/ g/ J8 qThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,0 j8 w1 K+ A) y" }
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
( c3 d& D1 x: e5 K% ?' D9 lcontext.BeginRequest += OpenSession;
7 g! a6 B3 q# a/ ^8 p% s.Bind(HttpContext.Current, SessionFactory.OpenSession())
/ a3 U: I6 Q+ y- ?2 C4 H2 o: }# }' k
//do transaction! h. F; y; g* A( G
getCurrentSession().Submit();
9 k! J0 y4 F8 B* d. \0 O$ | H1 X, ]getCurrentSession().RollBack();
1 v9 }4 s1 O* t) M- H9 |...
! \& J% x9 I1 I8 o* l% PUnbind(HttpContext.Current, SessionFactory)+ o6 h9 X6 q/ R9 I) P! [
0 w$ S0 A& V! y2 Rcontext.EndRequest += CloseSession;
' ]4 @& _% f& ]8 h$ l0 q, d. }
2 G+ t# d0 n9 i* F4 u9 i* q& P( I 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|