 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what. d/ ~- M) ]* | j& y: ^6 q
基本参数:
+ _# z& z& H" }; U1 v/ ijta | thread | managed | custom.Class8 O7 b1 n/ X5 G0 e6 w0 b/ w
主要是配合% M9 J* y# p! J2 x
SessionFactory.getCurrentSession(), R4 l* M0 y" b, I+ T
来用的.
w' y( H, F1 ]5 c2 A2 AThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,5 D7 S& d, i" m& M% L
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:/ b$ _. x0 U0 D L& M' ?, J
context.BeginRequest += OpenSession;
/ L0 U8 G& n( f.Bind(HttpContext.Current, SessionFactory.OpenSession())
3 t4 m" C; u8 R T% W$ Z$ {% Z9 N( R7 z5 f0 j
//do transaction
, {! x. ]* }3 _- _getCurrentSession().Submit();/ b3 \9 p- Y9 G( q( H" n9 ~
getCurrentSession().RollBack();
& x1 O$ `8 `) g$ w3 q+ [...
3 C, _( O& i; W2 jUnbind(HttpContext.Current, SessionFactory)
- f) y6 P# U5 w" A4 U. v
, K, {1 v( Y7 Z U7 U( _& Ncontext.EndRequest += CloseSession;$ K' e! h- A6 h4 w( G
8 g4 P0 }6 U6 V! i; y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|