 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
* V- \+ p. O. Q. p4 I基本参数:; d5 t/ z+ W8 Q4 y* n" [: j
jta | thread | managed | custom.Class* \: q4 V4 b0 q5 s% [1 }; h
主要是配合6 ^3 l* S7 v- ^
SessionFactory.getCurrentSession()
; N$ e" A3 p% d% t来用的.
\4 m8 ^) [, `' @! ?Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,/ q5 L: x& N( F: j1 f
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
* x0 C( F+ U0 Ycontext.BeginRequest += OpenSession;
0 `" r3 G% c- P6 v6 w4 l.Bind(HttpContext.Current, SessionFactory.OpenSession())! k/ s& x8 [1 q& Q! A. A" R
5 s, J* B, m2 B, w- z//do transaction
7 e4 m+ z2 E& H2 S# JgetCurrentSession().Submit();! D% q% m; k( D# j6 }: e" e. ]+ } I
getCurrentSession().RollBack();
7 t' j; r: |# `; f' ]$ t...
+ p5 W+ f3 y0 U6 {6 iUnbind(HttpContext.Current, SessionFactory)8 [0 Z6 n2 P% r: B
# k- h' c Y9 S1 ?6 n: w* y
context.EndRequest += CloseSession;
$ K/ K& t" k' e, n* Y5 p1 ~) ^
* b5 d1 r: X5 g- I3 S, ]& d 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|