 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
; _2 p* a; \' D0 ^基本参数:$ ~9 O; D$ v4 }4 n; [9 s
jta | thread | managed | custom.Class
% ~! P. g9 B% P: v主要是配合
, c& y& c1 b! b/ r9 I9 }SessionFactory.getCurrentSession()! \6 Z; M8 x' B8 e+ v0 n8 x( S
来用的.2 L! I3 [7 ]' P: R3 c
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,9 B1 ?* I0 d: P5 A6 d; k4 U- R$ C
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:/ [$ R7 }; p+ ` p" Y6 f$ E
context.BeginRequest += OpenSession;
1 @5 C9 b k7 c.Bind(HttpContext.Current, SessionFactory.OpenSession())
W) {; f+ ?& [. K4 L
& N9 Z: k; r7 x+ j$ J* M: Z v//do transaction
/ M6 o; X. G5 B1 PgetCurrentSession().Submit();
- k. ~" {; a6 m4 v1 n/ ~getCurrentSession().RollBack();
/ R- E; W6 \7 D! L3 A# j" U j# p...! x1 _" x6 p1 I& ?" W
Unbind(HttpContext.Current, SessionFactory) _' S0 G- m- M( f) Q L" C2 e4 b
+ Y+ W6 @4 Z; P6 K
context.EndRequest += CloseSession;
8 \0 G0 ?& }5 p2 [$ a) e
, D, [: z6 `/ U9 U- f6 _% e 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|