 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
# T# I2 y9 {( C: t基本参数:3 ^& I3 Q6 G7 O; j/ F
jta | thread | managed | custom.Class
; L+ B2 _: p4 _# x* h主要是配合! s* { w. F! z/ t: X8 o7 S
SessionFactory.getCurrentSession()0 a4 V6 N3 Z- q, l
来用的.
8 h; e/ L4 V- X2 n) C' k9 ^5 d, |Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,& t3 d! m8 R O. D) T# f: J/ A6 k
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
1 ]: b8 L) V; t: M6 v" C: p& ~context.BeginRequest += OpenSession;
+ c, z9 U9 o0 g2 ^) p.Bind(HttpContext.Current, SessionFactory.OpenSession())
9 N9 `, f% h: l- m8 `) C1 {/ G, p8 q+ w
//do transaction
- E5 b4 _) g7 e5 p6 d( HgetCurrentSession().Submit();
6 e! ^" w4 Q( |# ?2 OgetCurrentSession().RollBack();
+ k w+ C: {5 ?) I...
/ G# L, G7 z- MUnbind(HttpContext.Current, SessionFactory)
! V- n0 h4 G6 }5 _# n
' O7 w9 p1 h( z, X3 @" e$ D9 Scontext.EndRequest += CloseSession;
& U, O0 {/ w5 v8 n8 D+ N
! \3 [( E' A) Y* s 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|