 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
8 `) Z9 C; f' y基本参数:
/ [$ c8 f9 U0 s/ @8 N, P2 }jta | thread | managed | custom.Class
0 x) x9 l- g6 y+ r9 [4 z主要是配合
5 I2 i. c5 e, B O2 I( D- A7 `SessionFactory.getCurrentSession()
4 q1 N0 ]/ U9 d* I0 n% W; T" Z来用的.+ [0 `6 ]) `. n- c3 h% k* Z1 u S3 h! q
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
1 x; n& g/ \ @5 ~6 z' `; [; ZWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
! Q! L+ o7 r) V, E5 Y' mcontext.BeginRequest += OpenSession; R1 i% h6 Z- q, R( ^4 ^( V
.Bind(HttpContext.Current, SessionFactory.OpenSession())
) c; e2 V" y+ r; \9 j4 ~: P1 {6 K. M0 u6 p
//do transaction
$ M) h( Z V0 p6 t) E7 agetCurrentSession().Submit();1 O6 @) k6 a y/ d+ M
getCurrentSession().RollBack();! V5 l3 k' T# W8 C# [5 u) f0 U6 h
...- }" L' F1 J" i/ C
Unbind(HttpContext.Current, SessionFactory)4 ]& M8 n+ l8 Y: W f: U1 c: d
- k6 _& q5 C7 F3 J9 u" _context.EndRequest += CloseSession;
- x; S& V5 S% k$ S/ D' v/ m/ {8 T/ k6 x: ~* q: P/ g9 ~4 k3 Z
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|