 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what. E+ S0 l) u1 v& a6 T
基本参数:- G; A' o6 r* x7 L# c C7 O4 U3 \
jta | thread | managed | custom.Class+ O0 @5 o0 p5 S% G. V/ \
主要是配合
# |# ?( H0 G" R% r; |, W( uSessionFactory.getCurrentSession()
% K( ~' r0 Y& W! l( G来用的.+ E. d+ b2 C# @
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
4 U$ v4 |% ~: E/ E' O1 \8 G5 |Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
5 w9 D* t" u- u3 q, pcontext.BeginRequest += OpenSession;
, p1 f3 B F. v. {- D: z.Bind(HttpContext.Current, SessionFactory.OpenSession())
9 Q9 a3 r# I2 Q# G% I( q5 ]0 x
4 W2 i! E% b! e' J# ~+ ~//do transaction U2 b$ u+ s, g
getCurrentSession().Submit();1 e5 D2 l& g6 I& E7 o& d
getCurrentSession().RollBack();9 b$ X# v7 b& E/ U# ]5 w# W
...
) G- |$ u/ n& ^5 p; w7 hUnbind(HttpContext.Current, SessionFactory)
# m1 a8 m( _6 L+ o( j2 R. `' r/ z* K' W* a
context.EndRequest += CloseSession;& J* a; ?' ?5 ~3 o- h3 Z
' r4 F; j' k, ]# Y; q! ?( R" {/ N 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|