 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what$ U- i# {; y9 W/ c5 b+ G
基本参数:% A9 M" f8 k7 n' ^6 v: ~' o
jta | thread | managed | custom.Class
5 Y& r6 j& ~1 o主要是配合
& q" M3 Q! D' w+ ^3 ]/ _% P0 z6 j5 iSessionFactory.getCurrentSession(), {/ ~' }$ r" o4 ~2 J4 l
来用的.
+ w$ ~6 K8 C$ E4 jThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,: t7 G: }2 O1 R$ l
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:* W5 p+ }, t5 {9 R, c
context.BeginRequest += OpenSession;) P4 d+ ]" _ S* q3 d; N; U0 k* x8 y
.Bind(HttpContext.Current, SessionFactory.OpenSession())
; x+ p* t2 z2 R- S/ x0 V+ a
# v: \/ W5 T: `/ J//do transaction7 d( d3 z0 T# j
getCurrentSession().Submit();: x2 f9 n3 O/ H5 f$ ^
getCurrentSession().RollBack();/ s O+ e0 H# K5 [! a7 Z* P
...
6 ~# k/ U0 g* M; X# Y8 hUnbind(HttpContext.Current, SessionFactory)( _% i* }& N# U& N6 e9 M7 j" s0 ]
/ T% Z; S# r, W2 P# h
context.EndRequest += CloseSession;
! V! B" E+ r! r% ^
9 O- T9 Z8 z# N; D; c/ D. ~ j 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|