 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what+ U- A% F9 d$ `
基本参数:% h: |* R7 S+ E8 k+ a& Q
jta | thread | managed | custom.Class, f, m4 m. t9 z6 U
主要是配合) p+ E5 y) x7 J, o9 j' G3 O7 z1 G) T
SessionFactory.getCurrentSession()- X6 |' L& l4 F% c. g
来用的.1 R9 l5 @" I u* N+ I$ n
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% u( [& u% A7 O/ Z
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
$ \, k/ s5 o& n" v+ t/ d! ccontext.BeginRequest += OpenSession;4 w* L: E) f1 q& d$ R& h- q, r
.Bind(HttpContext.Current, SessionFactory.OpenSession())! \0 s e1 K% s/ B
( Z! r! }5 ]) e( ?! S
//do transaction
" y/ p4 g8 [1 f1 y% n, _getCurrentSession().Submit();
7 ~& x- r k3 i! MgetCurrentSession().RollBack();- w* e, C6 L; T
...- X, o2 J0 O Y- [# L% t% `
Unbind(HttpContext.Current, SessionFactory)
" _! P" l6 A# G+ d* q
; M% y. j5 B2 ]6 p% ~3 S$ Y3 Jcontext.EndRequest += CloseSession; P8 n* V+ Y5 [! m! \
% s! b( ^4 v1 a$ E8 f) l 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|