 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
) Q0 F( D- D! e# u" Z7 o* y5 @! V; S$ k基本参数:
8 i6 D% @0 B B6 s1 Q4 H0 r. ^' ~. [jta | thread | managed | custom.Class
8 w, G6 _: a, W( d1 B3 |; |7 C主要是配合2 l2 g0 d5 |& c: k) o7 y1 j
SessionFactory.getCurrentSession()6 O* C- W0 Q+ B
来用的.
8 a) _0 X8 V9 H- k, z, c& BThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,+ c; s, v! R9 \4 P' j% I
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
7 ]3 P# f. e1 b% }( Icontext.BeginRequest += OpenSession;
% G. `1 f ]' r2 q+ K/ Y.Bind(HttpContext.Current, SessionFactory.OpenSession())
9 q8 r, U& r6 W8 `
4 X. ]" j, I8 n' O( f' l, Q//do transaction
4 D3 L" k3 L% j+ xgetCurrentSession().Submit();
7 X7 h! {$ m- B+ N0 J1 hgetCurrentSession().RollBack();/ k8 \- b4 t+ g5 M: p
...
: W/ W. O! l) o4 e" T6 p# i* yUnbind(HttpContext.Current, SessionFactory)3 A* y. h- ^; A: [
) }( ]: o6 u/ b" M. d
context.EndRequest += CloseSession;- d7 R" e; j$ A% C- O% U0 d
5 N$ v/ A7 n' Z! f5 m" t% o
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|