 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what4 R% k) i; e: c! S
基本参数:
2 O0 J; R& V: |1 pjta | thread | managed | custom.Class
& Q: m, `( T: e4 ]主要是配合
; ~5 d. `4 c! l& g4 p- L/ KSessionFactory.getCurrentSession()
B) D% G# c' P7 n M6 |. |% v' W来用的.
; j' z8 F* z9 |Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
: S- L1 s, J+ A mWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
" C0 Z3 V1 h+ ~7 s. r4 Scontext.BeginRequest += OpenSession;
8 j; |. C, P8 B- g( ~' ?.Bind(HttpContext.Current, SessionFactory.OpenSession())$ Q+ x) \5 ]. n
' `- {/ u$ K+ t) u' ]' ^//do transaction. n* Q$ j1 y( e( G a7 [( G$ |
getCurrentSession().Submit();
) m/ u. k1 e5 B; |1 }+ b x% JgetCurrentSession().RollBack();! V+ K# F& w/ ]9 l% K0 g
...
( m. ^! I# ?0 J+ ` g' N3 ?$ B9 wUnbind(HttpContext.Current, SessionFactory)
! Z8 q6 t1 z9 I( R/ T+ q N
# |9 q6 R5 f3 F9 [context.EndRequest += CloseSession;
) G8 o7 t) _5 S6 c) E5 U' L' L/ @" R( T5 s2 ~7 O
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|