关于Scope,Compensation,Catch中的变量作用域!

流程
int g_i = 0;

int g_k = 0;
Message msg;

receive(msg)
scope
{
  body()
  {
    scope
    {
      body
      {
         g_i=1;

         g_k=1;
      }
      Compensation1
      {

         g_k = 2;        
         print2(g_i);

         print2(g_k);         
      }
    }
    throw exception;
 }
 exceptions
 {
    catch(System.Exception ex)
    {

       g_i=2;
       call Compensation1;
    }
 }
}
print1(g_i);

print1(g_k);

//--------------------------------

结果是:

  print1 print2
g_i 2 1
g_k 1 2

按照程序的理解,本来以为print1,print2都是2;

百思不得其解,后来查看BizTalk SDK Document发现这样一段话:

Inside a compensation block, all variables assume the values they had when the body of the scope committed. Note that the compensation block of a scope never runs immediately after its body completes; there is always intervening code. If any of that intervening code updates some outer-scope variables, and then the compensation block runs, those updates will not be seen inside the compensation block. Instead the variables will temporarily revert to the values they had at the time the scope which owns that compensation had committed.

补偿块的变量从body里继承过来,如果outscope更新了变量然后执行补偿块,那么补偿块里看不到这些更新,而补偿块里的变量仍然是以前body里的变量

 

感谢 工人 大力相助

ps:About Compensation,Transaction

BizTalk Server 2006: The Compensation Model

Transactions and Compensation Using BizTalk Server

posted @ 2007-06-01 13:20  upzone  阅读(350)  评论(1编辑  收藏  举报