 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
; }2 A) f% f; [( U基本参数:
: i0 C0 Y+ h9 _6 D, S$ u- H- _jta | thread | managed | custom.Class
$ S9 C. D. C: [. V) P# H. h主要是配合. T* j5 F# F2 h$ t
SessionFactory.getCurrentSession()0 L9 k. `+ O+ [. z9 |
来用的.
! ~+ u2 Y8 ]. OThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
8 {5 y! ]4 E; ?Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
9 a! _6 D1 \6 V/ econtext.BeginRequest += OpenSession;
% Q' Y+ a5 G9 R% M0 h.Bind(HttpContext.Current, SessionFactory.OpenSession())% \7 z2 y9 [' b' a9 b8 c
g T, v+ R( ~* _6 \
//do transaction/ u: z+ H5 D9 q- j7 x% m* s7 L
getCurrentSession().Submit();
: u' x* o a: F. g2 n5 FgetCurrentSession().RollBack();
2 m# W8 a7 o7 e..." a' q' q5 i0 k ~7 c
Unbind(HttpContext.Current, SessionFactory)2 Q% m9 q+ \/ V- z0 C+ a
; a. B4 e3 R+ s6 Hcontext.EndRequest += CloseSession;
% ?8 E, y9 T% a# t" G
2 S u# B& y" u5 K/ A0 B' Y 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|