 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what2 }2 c9 l8 P9 L) L4 v
基本参数:
2 U7 [, C( s0 M* a& ?8 D! {- Sjta | thread | managed | custom.Class
% d/ b; \; ^- ?* T- ^7 B# X主要是配合5 q* o! r' m3 N
SessionFactory.getCurrentSession()
+ ]+ E, Z/ o9 ?9 V' R9 ]' ~来用的.7 N9 G7 @: T+ j2 K. ?
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
1 _: }/ h+ c+ c( T4 b pWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
" `4 K6 g3 W( _0 J, b$ Ncontext.BeginRequest += OpenSession;& f; x1 E# y* y) k$ B ]) D4 M1 T
.Bind(HttpContext.Current, SessionFactory.OpenSession())7 X! M$ W# L2 h9 X$ F w
9 g( V) f1 e& U" L//do transaction
; n* [2 z/ z+ F! cgetCurrentSession().Submit();
% z7 R3 H/ O: k9 _' T) wgetCurrentSession().RollBack();: i. `8 z; }0 [% z" R, w' k7 C& e
...
) |- e# ~7 N) R/ ?* G) e7 }Unbind(HttpContext.Current, SessionFactory)6 N5 F8 J5 i! g6 ]8 h6 V/ b0 x5 m
Y. a) R1 I; F1 v; z; O
context.EndRequest += CloseSession;
. H7 _6 K1 ~% m; @6 U( _0 F' a
+ S6 Q( l/ j, x 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|