 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what9 _& l' j1 ?0 Y1 u; V% W
基本参数:
" @& z1 \; W, {; kjta | thread | managed | custom.Class
' ^; M& Z4 X8 _$ T ^主要是配合1 q; ^3 _6 s+ R6 \; @0 w6 m) C5 B0 ^
SessionFactory.getCurrentSession()* r6 `8 Z( @+ O/ C8 [
来用的.
# L5 `6 `' j }% x- f) A* v9 bThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
3 t8 t- c- C# b/ _Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
3 ?4 f' S( ?4 z1 ccontext.BeginRequest += OpenSession;
4 f4 ]1 V5 i0 s0 T; E7 j! {9 L.Bind(HttpContext.Current, SessionFactory.OpenSession())! S% s2 {6 d' L) P1 s9 {
! t4 g* ^3 z* S5 K( O0 D, o2 I//do transaction- z0 U- i( z4 ~6 Q8 U4 B8 u* ?
getCurrentSession().Submit();
! H0 X$ }# O9 {. g6 R! sgetCurrentSession().RollBack();
% V1 J" f4 U! A( y. t/ D...* R' i+ {$ [/ _
Unbind(HttpContext.Current, SessionFactory)
3 J. v6 V E% g& i( f0 `1 X
' H) [) `% A! c- ^0 K! h, Lcontext.EndRequest += CloseSession;- a! d H- \$ {& g$ N2 X; {& L
# w0 E( G$ \% b6 N2 F5 ~
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|