 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
/ J$ ~1 j1 \) \9 ]( U基本参数:
! y& _, l6 s" S" } djta | thread | managed | custom.Class8 K! `/ K) l: `0 \0 n6 R8 @5 b% H
主要是配合
) N' s; G5 w# W$ I7 i+ nSessionFactory.getCurrentSession()# Q0 H* k2 Z$ c4 u
来用的. L- r9 d& h. Z; M9 U9 o
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,5 N- M& H y. E# D* [
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
& I" q& A+ |7 _/ W, y3 E3 Ocontext.BeginRequest += OpenSession;
5 ^4 n( s6 t9 f.Bind(HttpContext.Current, SessionFactory.OpenSession())/ Z4 o+ m5 J3 r8 u% @
$ m0 N5 q/ i8 U: Y# O" W
//do transaction7 I, D; c5 ^; I0 E8 L2 [
getCurrentSession().Submit();
% p; E- i9 w9 X/ @getCurrentSession().RollBack();
2 x: a" w) E( j& P- L...
) j% a' v, U1 a; ^% A/ r" lUnbind(HttpContext.Current, SessionFactory)
# S+ J7 P. t- [% `( H: ~/ H
2 l, b! z) m1 zcontext.EndRequest += CloseSession;. T2 G/ j0 f. P; q# m! ~
; U. K+ v$ @3 C! |$ ~
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|