 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what/ v- ~& i/ h) r3 a$ T* p
基本参数: S6 `* G" }2 L: t4 j
jta | thread | managed | custom.Class
# |0 ]; I7 G- L, h; K主要是配合# @6 Z( k ^: M1 H% ]
SessionFactory.getCurrentSession()
' Y5 \( D# o8 ~来用的.9 F o# Y! j& {: L' m
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,! ^/ F3 Y3 N( V+ q3 N
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
s2 z, h" d- ?, u y+ x$ Z7 Icontext.BeginRequest += OpenSession;
7 w# v; f3 d. F/ f5 ^.Bind(HttpContext.Current, SessionFactory.OpenSession())
& o( t. m$ Y4 K* z& ]- S$ Y4 H" l/ Z+ ^; U! ^+ B2 v$ |( n
//do transaction
1 l) @2 Y; x$ u* k [/ }getCurrentSession().Submit();( e( U: c% {+ o4 Z9 J3 u7 U
getCurrentSession().RollBack();
! A, ^5 b- I( n; f* j/ Q d...
5 B+ t( h2 p" e7 @) K! n& b) ^* uUnbind(HttpContext.Current, SessionFactory)
( W. @. o, I: H. Q. h. n
/ l& H# B( K: m2 h6 q# R$ x6 Gcontext.EndRequest += CloseSession;
" m* D$ {4 b" m" Z$ w. B l, _/ y7 i* H
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|