 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
( V9 y* s& o2 i( v2 u基本参数:4 H Y6 K+ L7 l: t
jta | thread | managed | custom.Class' v; ~ D7 j- D
主要是配合
+ h+ u2 ^; S% H. p3 A. d4 N, ISessionFactory.getCurrentSession() ~& [0 d# Y, K) n F
来用的.
4 u5 c' M# h! D& PThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,. b- O( S) b$ ], e# A
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
$ @7 H8 g! S( D; hcontext.BeginRequest += OpenSession;
- ?& c2 }8 n$ Y/ }.Bind(HttpContext.Current, SessionFactory.OpenSession())
; X6 [/ ~3 M9 V: @8 R; x; Q9 H" [& p- g( Q6 o8 z: O4 W
//do transaction
) [1 Y# d& w4 J$ [getCurrentSession().Submit();
- D3 Y# ^. S; s0 h4 rgetCurrentSession().RollBack();
, J f7 M, n, n/ g( M) Q' L.... P4 z& f) p" t0 h
Unbind(HttpContext.Current, SessionFactory)
. o. v0 S. }& n$ n3 K+ |
% O+ ?' C3 k; Ocontext.EndRequest += CloseSession;
- Q; I/ Z3 r& w1 g8 s$ P# i" p
- r2 B& \4 X+ a 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|