 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what. L7 z8 d, W5 i( l8 b
基本参数:$ l$ ~, K* g8 g: k0 l& H+ {
jta | thread | managed | custom.Class
) g; g: P+ I4 D% E% o8 X @/ {. m0 \主要是配合
$ K: W% r0 Q! H0 `1 A1 @0 DSessionFactory.getCurrentSession()
0 r3 b% }# d, r, s: U1 u; H3 K来用的.0 `5 s$ ^+ E* f. d7 B! B" t, h# G* x
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,9 ~$ Z3 |/ p0 R# z9 N
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
, {* A2 v9 F( G1 P' h5 Gcontext.BeginRequest += OpenSession;5 B. N5 Z# b p* {
.Bind(HttpContext.Current, SessionFactory.OpenSession())5 g0 _) E( n2 k! q) u
. G; S9 o% `3 J* _//do transaction# f. F6 |: k: ]+ U
getCurrentSession().Submit();
3 o* p) E2 I# Z$ }' r, ~getCurrentSession().RollBack();
" V! d) N; Y( P1 l5 S...
! \( m) g* N) [4 Z6 x* ~Unbind(HttpContext.Current, SessionFactory)0 r) k+ B3 B7 ]& Q- Y; E2 m
% L- `9 n E% d; }' z
context.EndRequest += CloseSession;
: t4 f1 }9 r# b! T, \
% @; r6 ?& |6 |4 ^( c6 Q* a 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|