 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what3 l+ d3 _4 K$ U- v+ ~; C+ k0 H
基本参数:
w: W* q' k1 x0 ajta | thread | managed | custom.Class
: E. I. l7 R, R3 g5 _. Q主要是配合
5 ^6 @/ N2 P' gSessionFactory.getCurrentSession()( `* r& S4 s9 Z5 l, q0 m
来用的.$ q% H# N; ?# R+ t% [! {
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
- d. C+ X, W. B& n9 v9 h0 YWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:; ?$ C1 J3 M; ]
context.BeginRequest += OpenSession;
7 H5 Y3 A( h/ _.Bind(HttpContext.Current, SessionFactory.OpenSession())+ N3 R1 b5 C! s1 `5 t
0 O) C# ]0 F+ e7 R ~2 c* s//do transaction/ y% M {7 {9 O; m
getCurrentSession().Submit();
2 E/ v5 t$ ]+ u0 b7 A: y# X" jgetCurrentSession().RollBack();% ^) b- R8 _, s5 ]5 t U
.... |# p. f6 ~# R# ?
Unbind(HttpContext.Current, SessionFactory)
8 j' t% g- \0 K" ]0 `- Y9 J1 [
' ^/ u! I; g- H* Gcontext.EndRequest += CloseSession;
% o4 [# m1 H7 f0 B
$ q: {6 l W/ }9 V6 {6 o: f 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|