 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
& Y5 Y& Y7 {: K, n- J2 N" m6 o+ w' e基本参数:
2 h& U8 J4 N7 Njta | thread | managed | custom.Class; A5 b$ [: H9 W9 ?
主要是配合: `: P* j* W( M1 k4 N
SessionFactory.getCurrentSession()/ ^3 P/ o0 ?" m+ c' K0 p0 a; \
来用的.( g7 ~+ M/ a. K9 J& o5 L! K6 \! b
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
9 ?6 }5 T+ a+ o1 UWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
7 e0 W- e% s9 {! S7 [8 Ncontext.BeginRequest += OpenSession;9 D% a$ b4 p6 r* Q- i( l, r! g
.Bind(HttpContext.Current, SessionFactory.OpenSession())
& e' }4 T- @) f. z6 Z
$ `4 `) r0 _% w//do transaction
" r" L& W" L' [) W) C$ |# w" ]getCurrentSession().Submit();* L$ u5 ?- u1 ]; W
getCurrentSession().RollBack();
, p: i) N Y( ^- {; Q) d; n...0 E2 B4 R) o1 }6 W1 h% x: j5 X
Unbind(HttpContext.Current, SessionFactory)
; i; J I& g. p( Z9 m! C( h# Y
: q. ]6 p! D$ ]0 d; W1 D9 [context.EndRequest += CloseSession;
0 B, M2 ^8 h5 [, y8 O* f; ]7 u1 P
& X' Q; Y/ {( {, @" v 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|