 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
+ C' `& n& t3 P, v基本参数:
4 D Z/ N! [* D2 ?3 \, }0 c& Kjta | thread | managed | custom.Class# J5 K9 s2 m$ q" a' [, q: h, _
主要是配合# R( O) i; a" W6 P2 P }
SessionFactory.getCurrentSession()
( p# |7 ]$ [# f0 b8 \( ~来用的.% t9 X# n# C$ S& M: Q2 Y( {4 g a
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,$ p. D( R* U& |) c" Q: z
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
! d; }$ Y; d$ Y9 k9 {4 f& \context.BeginRequest += OpenSession;
% A2 e' F- r- {# t5 X2 ~.Bind(HttpContext.Current, SessionFactory.OpenSession())4 w6 x/ s2 ]3 |. u+ z5 s) I
. V% w b: z( Q" @* Z" {2 l//do transaction1 u4 C% s/ x- o" z
getCurrentSession().Submit();5 s/ c/ O4 n+ o5 `9 R2 Y
getCurrentSession().RollBack();5 k% U8 J( J G6 U6 {8 U; ~9 w
...; ^% w3 q, J7 q7 F5 [
Unbind(HttpContext.Current, SessionFactory)) n$ x5 h# I3 H, U, K0 A3 S
! A5 h8 H6 ^, D0 g( Kcontext.EndRequest += CloseSession;
2 K$ \% z5 ?" w9 i/ V1 _! M+ R
7 W, o( `* W' e/ L7 }+ m( E 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|