 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what2 j5 s. z* R' O$ k
基本参数:
0 j+ z% {7 D' x, x F! M) Pjta | thread | managed | custom.Class
4 S' `" V: W* E2 c; \7 t主要是配合$ \* r( K+ W1 ^: E/ i- a% n; G
SessionFactory.getCurrentSession()
, h1 i" Y- M% F9 \' a" w3 I' n来用的.
# m5 r* g7 J1 p# [3 sThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,8 j5 `! A) a5 }! F; C
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
! b8 ], r6 {( N; gcontext.BeginRequest += OpenSession;
5 a6 T/ G* {9 Y5 j- Q( [1 ^1 K0 b( r.Bind(HttpContext.Current, SessionFactory.OpenSession())
' o2 B- O* |# v9 j6 a% ?; j
' Q% T; R: ]1 }7 ~$ r. j8 S//do transaction
! e7 \+ L: N* N; E6 N5 fgetCurrentSession().Submit();
/ e3 p# N1 Y7 h, Y* y4 B' R) a S6 UgetCurrentSession().RollBack();# P+ ~' a# e6 i; E
...+ \0 ?& m" @5 g
Unbind(HttpContext.Current, SessionFactory)
c/ d0 j1 f, }5 Z
+ z8 z6 J& E0 ~% Y! Gcontext.EndRequest += CloseSession;
0 u: Q5 Q6 t. G' ^9 K4 E# k) a( l: e0 z! i% E) p
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|