 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
# X% T }; B+ t% f/ K基本参数:) Z0 L1 w8 k/ g5 B- a# `
jta | thread | managed | custom.Class
8 E- v# v( s% o! n) j主要是配合
3 N% d. G8 r4 D9 u- \+ FSessionFactory.getCurrentSession()
9 o5 r$ r, E& S3 J n+ U W8 G来用的.* u* B2 I6 G* y
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确," R' |8 O; a! o; k% }: H
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:' X: W) j5 k& f: R5 e, j
context.BeginRequest += OpenSession;3 ]6 k5 c! V7 K! K, c; S
.Bind(HttpContext.Current, SessionFactory.OpenSession()) h# O; X8 f8 K7 j0 k
+ W9 B- I( L* l! e% t! f
//do transaction
) U! K: I, z% |# ogetCurrentSession().Submit();
- s t2 Y6 z8 YgetCurrentSession().RollBack();( ?9 q( p: |4 w$ @$ b+ x3 |( K% c0 q
...3 h' [+ U7 t9 f5 m+ P
Unbind(HttpContext.Current, SessionFactory)
7 z9 _; [3 g- }4 V# N) u" D, X4 A7 b6 x8 B3 h, X, f1 a
context.EndRequest += CloseSession;) s; r8 C' A0 F0 I
$ v/ m) f" X |& }- e/ } 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|