 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what9 ~ `- S" j5 J: |! a! R9 p/ M
基本参数:. C' O J' u- u9 t$ M o
jta | thread | managed | custom.Class/ b0 o: j+ k# a5 m2 n
主要是配合; \" e* \& l0 h# Y6 o, R6 H
SessionFactory.getCurrentSession()$ j4 @' k4 x6 j
来用的.3 C4 [1 g* y8 L* z0 l5 r
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,! `! _6 T. d- J0 L8 W$ `
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
: z. G# p! _/ r# S& Q* rcontext.BeginRequest += OpenSession;2 ~) l7 _& O! F7 `( E8 X
.Bind(HttpContext.Current, SessionFactory.OpenSession())
& o" i$ M% z6 V% w$ J# `/ x+ S* }' `& U, z4 x1 f
//do transaction, q: d, a4 V5 k3 i
getCurrentSession().Submit();! q6 I1 T, ~. Z8 k, m/ g/ o
getCurrentSession().RollBack();$ A" N; ]. v9 P" E* C. D
...
Z' |+ w9 k, u% ~9 JUnbind(HttpContext.Current, SessionFactory)
G) C( _8 | _7 S5 ]$ e) d" p7 q; e( A/ I1 U6 @
context.EndRequest += CloseSession;& C1 M, K- k$ L( R: d, }) E! z4 E
7 l. m* K/ ]0 c0 L
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|