了解transaction.complete的功能如何?

new*_*w14 3 c# distributed-transactions

大家好我们已经开始使用事务范围,下面是代码片段.我们需要了解的是,在每次使用连接后,我们的底线是否会处理/关闭特定连接?所以,自关闭以来,transaction.complete如何运作?

using (TransactionScope transScope = new TransactionScope())
{
   try
   {
      string myConnStringLocal = "User Id=***;Password=****;Host=" + globalSettings.settingLocalIP + ";Database=" + globalSettings.settingLocalDB;
      using (MySqlConnection connectionLocal = new MySqlConnection(myConnStringLocal))
      {
         try{
         connectionLocal.Open();
         }
         Catch(Exception e)
         {

         }
         finally{
         connectionLocal.Close();
         }
      }

      string myConnStringCentral = "User Id=***;Password=*****;Host=" + globalSettings.settingCentralIP + ";Database=" + globalSettings.settingCentralDB;
      using (MySqlConnection connectionCentral = new MySqlConnection(myConnStringCentral))
      {
         try{
         connectionCentral.Open();
         }
         Catch(Exception e)
         {

         }
         finally{
         connectionCentral.Close();
         }

      }
      string myConnStringCentralCopy = "User Id=*****;Password=*****;Host=" + globalSettings.settingCentralCopyIP + ";Database=" + globalSettings.settingCentralCopyDB;
      using (MySqlConnection connectionCentralCopy = new MySqlConnection(myConnStringCentralCopy))
      {
         try{
         connectionCentralCopy.Open();
         }
         Catch(Exception e)
         {

         }
         finally{
         connectionCentralCopy.Close();
         }
      }
      transScope.Complete();
      Console.WriteLine("Transaction is completed");
   }
   catch (Exception)
   {
      Console.WriteLine("Transaction is rolled back");
   }
}
Run Code Online (Sandbox Code Playgroud)

Rwi*_*iti 6

TransactionScope.Complete告诉所有交易经理他们最好提交此交易.并不能保证一切都会真正提交.调用Complete方法后,所有事务管理器都单独启动提交,如果所有事务管理器都成功,则认为事务已成功完成.您可以参考此链接了解更多详情

  • 你永远不能保证提交.交易的目的不是保证一切都会保证一切都会提交或什么都不会提交. (3认同)