 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
4 s( [+ z* r, E( u基本参数:
* r! y8 S4 M; a4 \jta | thread | managed | custom.Class- |: L, w" c9 m }/ G0 {7 \8 Z
主要是配合7 q5 @; I: I+ J4 ]6 J9 r5 t/ k
SessionFactory.getCurrentSession(), _# e, n& R1 d' w# E3 W0 O
来用的.
* c$ B/ b4 G7 S) L( EThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% d* b# w8 y7 j, Q2 |
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:) [3 B0 G+ n! z- D3 q
context.BeginRequest += OpenSession;3 T0 B: d% u6 k- }5 a
.Bind(HttpContext.Current, SessionFactory.OpenSession())) X+ h; {/ y! |
& p6 B' }# s j+ v
//do transaction
' D4 r3 J7 @) S6 f( O* Y: N4 GgetCurrentSession().Submit();, ~9 g( a1 P: k$ N8 z
getCurrentSession().RollBack();
; C3 Y/ j T, n8 Z6 \...
+ _- w9 x& B7 j ~Unbind(HttpContext.Current, SessionFactory) ~; Q. v) e6 ?
( C) W$ y$ Y* ?2 _! F2 f, e
context.EndRequest += CloseSession;
( k2 P- W5 l2 F+ b/ G. V" f' d5 E+ \ Z$ I
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|