 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what6 }) t' P0 T! ^
基本参数:
) l# Z2 n' b( n8 S4 M8 Q8 V* g) p2 G3 njta | thread | managed | custom.Class
5 J2 g/ J- M/ D6 @主要是配合
+ ?" K$ r8 \0 ~/ b# SSessionFactory.getCurrentSession()
5 }* [& K" Y) ?7 ]5 a9 w来用的.
5 ?% U8 {. s* G& P! L6 b: fThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
& b) n6 U6 o+ r( u7 oWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
0 u4 d t) d/ i- \: e, Icontext.BeginRequest += OpenSession;
$ A) o4 o5 i+ t* I) x2 a/ r.Bind(HttpContext.Current, SessionFactory.OpenSession())
$ q) D N4 }' Y' j. Y, W2 O
4 W& @' m7 H, ]& |& |9 @//do transaction: o1 {2 X/ K/ _( w0 U7 y2 \
getCurrentSession().Submit();
/ t. d, N' C+ f. s% d& MgetCurrentSession().RollBack();( S- d, `, W7 k F: @, H
...
$ u# P5 a' Y" R0 RUnbind(HttpContext.Current, SessionFactory)
- y" L1 Q7 r! H# w* j9 c+ |7 m3 C# D H+ Y
context.EndRequest += CloseSession;
. j& {( P3 B4 X, V! d$ r
) Z9 A: i3 g! g2 y8 s* \, @& A 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|