 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
" w; Z# g1 G; l基本参数:
, T$ t5 s+ t% C9 t5 m: Jjta | thread | managed | custom.Class" l; e$ h* x/ O; T* z* a$ {
主要是配合7 L! f! m$ _% |! L
SessionFactory.getCurrentSession()
* p. a" R* }9 l& k& G" y8 o来用的.' v% I0 V1 E9 Y% G+ ]
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
% v3 i& \9 O3 w7 X5 zWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
& ^ c; ?0 `8 ~, _& ccontext.BeginRequest += OpenSession;2 s2 _& f5 b* m/ ^
.Bind(HttpContext.Current, SessionFactory.OpenSession())9 [6 ?% ?7 H6 B
/ P! u M! t+ Q1 s6 F% a6 i' @
//do transaction C% X( y3 Y9 Q3 I; k, E3 w: E$ H
getCurrentSession().Submit();
2 H/ r! x/ o* \ J$ VgetCurrentSession().RollBack();
1 \$ H- W/ V' p; V...$ V* E, R9 @" W9 ]7 X4 B
Unbind(HttpContext.Current, SessionFactory)
* a- A. I2 Q! m- I( U7 x6 m4 ]# G) T3 @9 r; z; `: _! f
context.EndRequest += CloseSession;
: ] E0 d; g& t3 a1 J- f( O7 Q% f/ K0 w. `' N. E' E
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|