 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what6 r) k* t' p4 p9 @/ x1 Z$ Z& H
基本参数:1 q2 u6 ?8 O: ?+ y
jta | thread | managed | custom.Class
& G2 Y8 K4 Y1 ~8 F. B; M主要是配合
$ @' B/ [' k$ T4 u" o7 T# |+ |SessionFactory.getCurrentSession()
% v/ C. D: k: z% ?* a7 H! y来用的.0 I) j% U2 G4 q; q0 z4 N5 _
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
* n& ~- A" d C* s, u8 cWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
0 a- o8 N1 v* ~8 i3 Z; Y* Kcontext.BeginRequest += OpenSession;4 e# |3 y5 ^$ }1 H0 ~0 e
.Bind(HttpContext.Current, SessionFactory.OpenSession())
0 j6 i/ f0 v& _& g5 @: ?9 W# a9 O A6 D& }
//do transaction
7 k5 e, n: ?* F6 h2 _* sgetCurrentSession().Submit();
( r' f1 @ g) F" a: _ z0 g4 _getCurrentSession().RollBack();+ q5 P6 \% N# q& O
...% H; v U& U* ^! d" T- x. m
Unbind(HttpContext.Current, SessionFactory)
& Y) F; R) o: m8 M% u
; a# w+ a$ o4 ucontext.EndRequest += CloseSession;, r/ T6 i+ ~8 F2 Z7 m. b' d* w
1 u+ ~% Y$ `+ f* h
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|