 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what4 D1 r) Z b+ i& y
基本参数:# i" C w d$ `5 U; v; ^; D* X
jta | thread | managed | custom.Class
9 o5 F* }6 L: N主要是配合. i' M6 b0 L* _
SessionFactory.getCurrentSession()
& c3 h# m& m. h% t$ G来用的.5 y6 L8 \) O3 z2 h+ e
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
; ~' ~ n. v% y9 S+ kWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:( X. O) z* c5 k5 D( H' w
context.BeginRequest += OpenSession;$ [6 o3 ]6 r* R' O
.Bind(HttpContext.Current, SessionFactory.OpenSession())5 M4 o- C4 a- b" K1 o+ ]
" A; k1 N" {0 u2 X2 L' Z//do transaction# k, T+ l! f: y' }6 n, W1 N* F3 T4 O: M
getCurrentSession().Submit();, v, `1 _) U& I- O# C3 O3 A
getCurrentSession().RollBack();& w z6 Y3 F1 w
...
$ h8 B, ]. f" UUnbind(HttpContext.Current, SessionFactory)
9 N7 M! ?9 w! y$ P( l9 o {. h- u6 y, {, ?% E! `- I3 O/ g$ S* m3 H
context.EndRequest += CloseSession;
( p3 m3 q6 I( u6 R5 L- V/ a l# Q" j8 [, C1 \5 g* @
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|