 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
. A% A2 W3 z( b! H基本参数:
7 }5 F$ ~# O! Q' V# U/ ?5 O, fjta | thread | managed | custom.Class' F+ ~$ X7 n/ ]- ^$ k j
主要是配合, a. h. ^! y* {. ~$ z% _
SessionFactory.getCurrentSession()
& ]6 y0 `* U' v4 ~; D来用的.
5 q6 Q- V% H5 T$ M+ GThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
) G5 O8 r0 N" N1 l7 \; P ]Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:, _) m3 g$ d& ~' h" z
context.BeginRequest += OpenSession;2 W2 I" P7 c" b/ ~
.Bind(HttpContext.Current, SessionFactory.OpenSession())' I0 Q9 M) P# w
5 ?$ `4 B6 m# \//do transaction
- q' r# Q/ ]/ r0 @1 IgetCurrentSession().Submit();
3 G- t* E5 V9 |getCurrentSession().RollBack();
8 A; K% u& W' I, ] {4 m" W...
& v9 }( @/ B$ ?2 ~Unbind(HttpContext.Current, SessionFactory)
! P+ G7 O+ }! c8 t4 l3 W' G; G4 {2 M
context.EndRequest += CloseSession;( l0 M& ]( b& ^
1 `2 ]/ o5 P1 L. }6 d 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|