 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
. S# y' f$ t6 x, D/ J+ L! J基本参数:5 l9 \1 t$ [( ]9 T" _0 H
jta | thread | managed | custom.Class
) v, C5 b2 f1 v' ]5 I* L/ n主要是配合
. r& c6 o- M: [* ?$ `SessionFactory.getCurrentSession()
% B8 I$ ^# x" T& c* Q7 e来用的.
* P: L+ a4 M8 aThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,- {2 ]# Q- d% ~7 a$ x+ @
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:. u. V6 C( C3 `- ^% Y
context.BeginRequest += OpenSession;3 R; y6 L7 Z" ^' K
.Bind(HttpContext.Current, SessionFactory.OpenSession())" q% d0 h3 y4 N; I- \# U4 x
% ?3 x4 d6 o# ^4 j$ N2 S
//do transaction& T9 ?* m6 m1 n" Q t: x$ F( s
getCurrentSession().Submit();$ X- }: i+ n8 q# H( l3 j
getCurrentSession().RollBack();
5 f+ h4 l4 ~/ |9 v- q/ k! k... l/ k1 s: f7 Z3 L
Unbind(HttpContext.Current, SessionFactory)
9 l$ {3 Q1 }+ m2 I1 _$ g5 t7 c) m5 i+ S
+ }0 U, W @4 z9 ncontext.EndRequest += CloseSession;
g$ b- Q. j6 ?. t3 }6 n
) J5 Q6 }" a' h3 [/ Y. @2 f 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|