 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
0 W, k$ {* W& e: d基本参数:8 D* D6 j5 i6 z' X8 o# q
jta | thread | managed | custom.Class1 S( C# z0 i0 C( z
主要是配合/ U6 @( i) C" L8 \
SessionFactory.getCurrentSession()
/ E( t) g% [1 h: h4 \# W, Q' |( c% P来用的. N' b6 T3 l# g* h, v$ A
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确," y4 S: s U6 U8 v
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
5 A' F( y6 Q3 e+ V+ c1 q" t" Z: Dcontext.BeginRequest += OpenSession;
+ N+ Z0 K3 v/ k2 y3 H3 w.Bind(HttpContext.Current, SessionFactory.OpenSession())
# J" y5 t" s$ O% u- p
S/ C' N! }. k( e9 ]# V//do transaction
3 W' @& t$ }) `getCurrentSession().Submit();6 F X2 d/ u9 c1 ~
getCurrentSession().RollBack();
+ J3 y1 L/ k( |" v7 r/ [...
2 C- K) r8 n2 v/ cUnbind(HttpContext.Current, SessionFactory)
; f& X" ^% E) r: D" M8 a8 g1 [* M J1 ~4 Q8 X# ?7 x8 p
context.EndRequest += CloseSession;
( {( m7 |0 y. ~( @
4 ?; @* n7 p; m: N5 N 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|