 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
2 w% {+ \2 q6 n& y- K) Y4 j; ]基本参数:* x/ B2 s- L, h" G' {0 T% @7 |
jta | thread | managed | custom.Class( @2 E- X8 R' V9 p
主要是配合
: b6 E$ Q7 t' t$ B% \# O% jSessionFactory.getCurrentSession()4 q% p" r( X2 a1 N' w) j
来用的.
- N$ w9 v& _% F2 Y1 {, O* L' BThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,) W; w7 O4 P+ d' t
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
# `. h1 ?& C% P- Qcontext.BeginRequest += OpenSession;
6 S, q) q( T1 h$ y# D# G9 m.Bind(HttpContext.Current, SessionFactory.OpenSession())
7 ~' A+ a, m8 X9 w- X& w x5 ]: F" I' p1 v
//do transaction
0 l' W" V2 M) a9 ygetCurrentSession().Submit();
4 F2 I) y, x7 mgetCurrentSession().RollBack();8 b1 \& d2 D% K# P
...5 K( i: z i) H* k2 E* h
Unbind(HttpContext.Current, SessionFactory)
9 \: K3 Y/ b7 L2 J# i( A3 C8 R" v. J$ r3 G! A9 i# C8 X4 O! k& ]" F
context.EndRequest += CloseSession;" P- c# }9 ^" y# m5 O- @. S6 _
0 X* w' D% ]) r) Q- q
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|