 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what) n# Z3 O6 {! l: X) y; { d
基本参数:% b# g& @( ~3 w1 _3 Q3 ^, t
jta | thread | managed | custom.Class- X* @) `2 w, a
主要是配合
% }9 b% W3 r" l: v# O- k) VSessionFactory.getCurrentSession()
' C3 C2 G! V. _" p来用的.2 o" v6 ]& y; R3 H+ M* p
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
$ O% a$ b4 @% F1 v! e# OWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
1 y3 {4 d9 ]0 {context.BeginRequest += OpenSession;2 I4 F) s# i2 P! \1 L! _& E
.Bind(HttpContext.Current, SessionFactory.OpenSession())
6 _$ | n4 g8 s$ B! B
8 M% B0 \! |) t//do transaction6 o, V$ ` a. W3 _6 l) u/ s( D
getCurrentSession().Submit();
0 Q6 z- j7 @8 }5 N; x' p) I9 TgetCurrentSession().RollBack();$ m: a- } ?4 H
...; u& S( w' p; d+ t+ ~
Unbind(HttpContext.Current, SessionFactory)
7 ^' {# z, L( E" H: Y( L9 i" U* ?8 Z# q! Q7 K$ w f2 y2 W2 b% b
context.EndRequest += CloseSession;
. v& G- w+ P. v' z9 d6 D% Z6 p: W; k3 K5 D5 f
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|