 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
" O$ }/ E( B: j9 o4 B8 q基本参数:
8 ]& u- \5 [; a4 V" n$ I' Ijta | thread | managed | custom.Class
/ ~, z6 B5 @9 t6 |# \- `' Z主要是配合
; j3 i9 p" t2 uSessionFactory.getCurrentSession()7 I+ D/ F8 ]% \& X
来用的.8 f5 C; O, B+ Q9 H$ h8 Y% R8 [9 n
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,0 }9 Q- L" q: \
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
: g" \( x0 _) H2 b# m0 w. z6 m6 Ocontext.BeginRequest += OpenSession; ~- \' O: ~; f. V0 T" k
.Bind(HttpContext.Current, SessionFactory.OpenSession())
; T/ N( `8 l8 ?6 {- k/ M+ x' ?
4 j5 J- T0 K0 Q//do transaction/ F- \/ Z& L* i9 w, _% p% X- g
getCurrentSession().Submit();
8 e& J1 r& S& u9 A7 c' f- Y# FgetCurrentSession().RollBack();
! N2 B+ O; L Z) k6 {$ i7 W...
; y/ L7 V( u! T4 G$ o# \Unbind(HttpContext.Current, SessionFactory), c& O' c! Q h2 f" T5 D
8 Z: n: I: L. U$ }- F+ W
context.EndRequest += CloseSession; Q' d0 A7 F6 j/ V( ]& a8 Y8 G
o6 F* k' P* s& V$ ]& l
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|