 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what! r3 `, I3 i6 a( D8 @
基本参数: X5 k3 Q3 h$ M+ q! V8 I0 s5 `; t
jta | thread | managed | custom.Class0 U3 q0 T* v+ L' I$ B, V) h
主要是配合! ~' h9 g/ R/ k! }# {
SessionFactory.getCurrentSession()0 I O) a5 F- K; Q
来用的./ }* ]. d. C. c& H6 B! T
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
9 j& z8 f+ c1 ?( z3 H- `( KWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
6 z- Z! C6 @6 [) i* l8 _2 Vcontext.BeginRequest += OpenSession;1 G( Z$ q# E u( \3 k" E6 f
.Bind(HttpContext.Current, SessionFactory.OpenSession())
. w; y& F2 n5 i8 V1 @( v. M! J1 W8 b6 {4 G1 x0 v, h. f
//do transaction
4 Q! v: T/ o# I% L3 t4 GgetCurrentSession().Submit();
8 U5 T4 h7 } lgetCurrentSession().RollBack();
V" i5 R% y# V1 f5 L1 K1 J...
% v7 I- E6 N" ^4 p, f4 ZUnbind(HttpContext.Current, SessionFactory)% \; y& c/ j( s, `/ ]
! e* j! V0 x7 S# O9 d# |
context.EndRequest += CloseSession;' E2 D, G4 c& w/ P) k* ~4 k
. ^. z5 `/ ^& N) I' I2 S- \ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|