 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
! B. O$ J- I( B/ V- J* |基本参数:
3 K8 C1 l% A$ b% I+ ]jta | thread | managed | custom.Class
& \! Z: p& z5 N1 U7 @6 [- |主要是配合
3 _1 e/ c( B3 U# K: S9 h, d$ PSessionFactory.getCurrentSession()
# }) K8 h5 `) Z) e. i( P6 K来用的." X- R- y. Q. Y8 b5 M C
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
4 z4 E$ y. V0 N |9 G9 D" dWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
# S8 G/ { F; B! ~+ M6 c% scontext.BeginRequest += OpenSession;
" \5 F. ~- t8 u7 M1 H/ G/ K1 x.Bind(HttpContext.Current, SessionFactory.OpenSession())
6 K4 T, A! e6 @1 Y% l Y! [
# V( a' A" B/ _4 R- J//do transaction
( P! T$ u2 X- X, Z" l6 hgetCurrentSession().Submit();% r P$ h$ I; y) h8 H
getCurrentSession().RollBack();) s2 k9 h) z; J& `
...
5 J! c$ m5 r% V. b3 YUnbind(HttpContext.Current, SessionFactory)
* m0 O c/ H8 i9 ~
* L u+ k0 X7 f% M2 C. Wcontext.EndRequest += CloseSession;7 Q K6 G0 a- k; @5 a3 h
- q9 ^9 f9 h3 C; `: Y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|