 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what! ]" r1 D' w. t
基本参数:
% M. C& v; N' gjta | thread | managed | custom.Class: X7 A; |3 {9 [4 J. j$ s
主要是配合
$ C5 k; E, X* a( Z' JSessionFactory.getCurrentSession()6 l, i$ {- L5 b6 W
来用的.
& g! q$ {. c0 p' L7 J4 yThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
; M9 X6 z2 C BWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
' f! W8 H! M7 M8 Mcontext.BeginRequest += OpenSession;8 s; S/ f/ f7 M* k |: K; J
.Bind(HttpContext.Current, SessionFactory.OpenSession())( P) t7 G7 Z3 _% }1 e% v9 \
. [, o! _ I7 v& ?, O3 ~//do transaction
8 k( B% ^* B. Z2 u/ L8 _getCurrentSession().Submit();
3 I. N( i: n- I; N3 ZgetCurrentSession().RollBack();6 U6 H6 A$ K$ C/ j; t
...
. ~0 j0 e! T: o* b2 z% M+ m" E$ t3 VUnbind(HttpContext.Current, SessionFactory)) e6 E, j; i( s9 K6 Y) X
2 |' ]: c6 O* i0 P; Vcontext.EndRequest += CloseSession;
$ m9 o# v+ K* j5 `% [ s7 h
/ y% A+ U) A7 R; E1 z" f3 O8 t, G4 f 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|