 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what/ w3 {# }, t0 A& i
基本参数:( q9 j( i; ^/ x- y5 t4 m. ]
jta | thread | managed | custom.Class
* G% i) o! x0 |% L5 j主要是配合. G) q$ _: V3 M' d; Y8 I
SessionFactory.getCurrentSession()0 w: z$ q" b3 s2 e Y4 O* g
来用的.# v6 Z' F5 s# f0 T
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
# Q# u4 P$ x' xWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
& _1 y* t5 |4 b* \: i4 v/ X Scontext.BeginRequest += OpenSession;
; ~" Z" h1 k, [+ L.Bind(HttpContext.Current, SessionFactory.OpenSession())
- r- y L# k( q2 ]0 S7 f
3 K: }. R0 o+ j. H//do transaction
7 ~4 z% Z* q6 |, C0 S% GgetCurrentSession().Submit();% t% }1 p. n7 q. @3 N
getCurrentSession().RollBack();
' K# H3 @* X: V7 H) X9 Q...
6 s/ _, e& U6 ^3 U2 J0 ^8 G- gUnbind(HttpContext.Current, SessionFactory)
1 W, u. x/ _5 ~! S( L4 Z1 a$ y8 m: c% O% {4 u
context.EndRequest += CloseSession;: c& W( `( l& X X t
5 @4 @- u/ Q: L3 x9 P
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|