 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
2 a4 F8 z! N2 G% T0 W基本参数:6 E) k, E- P' o, b- \
jta | thread | managed | custom.Class
5 D: j8 A7 Y! y+ t* t6 N9 H主要是配合
1 V; t6 ]* ]+ bSessionFactory.getCurrentSession()
0 Z) \ u) d* _! r: j% ^7 _来用的.2 `. s) K: z6 I% x/ X
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,/ q# h9 C$ N' p/ X- u; q! L- b8 G& h
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
6 i% M0 m$ ^( h9 |% ncontext.BeginRequest += OpenSession;
5 r: o {8 w; o# J5 i.Bind(HttpContext.Current, SessionFactory.OpenSession())# I$ d. b$ f2 D O9 b
0 b% X7 ]6 [+ x
//do transaction/ q+ C( I; Q# I6 i0 d- s
getCurrentSession().Submit(); ]# x! k9 {$ v. w8 m
getCurrentSession().RollBack();
2 ?8 J8 s( J+ Y3 M; P; _$ [8 T...$ }9 m, _- J6 I6 {+ O
Unbind(HttpContext.Current, SessionFactory)
0 q$ o) [9 x- [' ]9 F, U6 G+ ]+ T6 }4 J+ r. `/ |
context.EndRequest += CloseSession;$ I& L, U" X& A2 o; J
/ ~4 P! D+ j' \$ ~7 {, Q 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|