 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
2 S3 S% h1 q x9 g5 }$ |7 Y基本参数:( i/ C% W- M9 D
jta | thread | managed | custom.Class
$ T- p- K' K( L. V: u. x主要是配合
* K: g) r. h) r4 WSessionFactory.getCurrentSession()
: v F- _7 @' |, m3 a( F来用的.
. T3 P/ @' f9 V0 r+ wThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,, J2 @8 U# w9 [$ Z) Q- J$ k
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:2 C2 Y" u$ K, f. U3 t
context.BeginRequest += OpenSession;- q0 N0 ~2 B6 t% r7 t
.Bind(HttpContext.Current, SessionFactory.OpenSession())8 L) ]- \- }3 @) Z
( u0 [ t$ r/ D+ i//do transaction
) J: ]0 b9 x+ igetCurrentSession().Submit();( N( n! Q I* g+ V4 F- ^1 ~$ B# O, e
getCurrentSession().RollBack();
+ {: S; X3 K9 S8 ]* r, ~* i...& \0 C. Y' m7 O
Unbind(HttpContext.Current, SessionFactory) i4 @% F$ Q, D2 g& E
5 b( Q3 P8 q8 j1 Q
context.EndRequest += CloseSession;' z( X0 D; f0 N1 B& d0 H% ]/ q
! p2 f- X$ Y3 M2 @; _; X6 c
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|