 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
' U/ C, g O# P0 z基本参数:
! ?# I( I7 ?& `$ w/ njta | thread | managed | custom.Class: S$ F9 g6 h3 E
主要是配合
. g6 H$ p& C$ V; G) L9 V% TSessionFactory.getCurrentSession()7 v4 \8 Z4 Z+ U( W7 f
来用的.
2 e; g2 X9 X0 @( r8 A9 a) a: }, hThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,9 Q0 w6 [5 l$ V& i$ ^
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:( G! G' L/ ]) B- b' ~
context.BeginRequest += OpenSession;
+ [0 a) B2 S# Q7 E+ T.Bind(HttpContext.Current, SessionFactory.OpenSession())0 o0 `- l6 z9 Q" f0 ^9 a9 G6 `
3 B& ]4 s. \: W. Q; z//do transaction. M7 }$ Z# h: v% q& v1 l3 N+ ?
getCurrentSession().Submit();
; c# _1 F6 r6 S2 B ~getCurrentSession().RollBack();, t' Z9 ]" D1 g2 f- s
...+ |# H7 e5 M% y* N( A
Unbind(HttpContext.Current, SessionFactory)- h" g# k% `) e1 o
) G& d; j d7 ^# @! [; Zcontext.EndRequest += CloseSession;. [6 g: v' E0 I+ Y, K! W% \" K, R
2 h) T" }" u- G- u8 l; t( \" ?( M 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|