Question

We have been using C# and mysql with .net connector but with individual commit where at times it fail to commit all. So we are now moving to this tool http://www.devart.com/dotconnect/mysql/ which supports distributed transaction. All works fine just that we are not too sure how to put the multiple connection. Method 1 is nesting each of the connection into one another. Method 2 is separately. Where we should be closing the connection or is handled by the transScope.Complete(); and transScope.Dispose();

Method 1.

using (TransactionScope transScope = new TransactionScope())
{
     string myConnStringLocal = "User Id=***;Password=****;Host=" + globalSettings.settingLocalIP + ";Database=" + globalSettings.settingLocalDB;
     using (MySqlConnection connectionLocal = new MySqlConnection(myConnStringLocal))
     {
       connectionLocal.open() 

       string myConnStringCentral = "User Id=***;Password=*****;Host=" + globalSettings.settingCentralIP + ";Database=" + globalSettings.settingCentralDB;
         using (MySqlConnection connectionCentral = new MySqlConnection(myConnStringCentral))
         {
             connectionCentral.Open();
               string myConnStringCentralCopy = "User Id=*****;Password=*****;Host=" + globalSettings.settingCentralCopyIP + ";Database=" + globalSettings.settingCentralCopyDB;
              using (MySqlConnection connectionCentralCopy = new MySqlConnection(myConnStringCentralCopy))
             {
                 connectionCentralCopy.Open();   
             }
         }
     }

     if (rollbackBoolean == 0)
        transScope.Complete();
     else
        transScope.Dispose();
}

Method 2

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

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

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

    if (rollbackBoolean == 0)
        transScope.Complete();
    else
        transScope.Dispose();
}
Was it helpful?

Solution

Both methods are correct. TransactionScope will be used together with dotConnect for MySQL in both cases. The following code is not required:

else
transScope.Dispose();

because the Dispose method is called automatically when exiting the using (TransactionScope transScope = new TransactionScope()) block.

... I went through it meaning in my case the transactionScope will close it when I call the complete rite...

No, the transScope.Complete() does not close the connection. If the connection is closed inside the using block for TransactionScope, the connection object will be closed, however the internal connection will stay open for sending changes to the database when calling transScope.Dispose() if it is preceeded by the transScope.Complete() call. transScope.Dispose() closes the internal connection if the Close or Dispose method was called for the connection object. If the connection object was not closed, transScope.Dispose() does nothing to the connection.

... Another thing in each of my connection I keep track of try and catch and if there is any error I flagged the rollbackBoolean to 1 so then it wont complete and whole transaction should be rollbacked is that a correct mechanism? ...

If an error occurred, just don't call Complete. If the Complete method was not called, the transaction will be rolled back when executing the Dispose method.

Here is the example with a try/catch block and rolling back the transaction in case of error:

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

      string myConnStringCentral = "User Id=***;Password=*****;Host=" + globalSettings.settingCentralIP + ";Database=" + globalSettings.settingCentralDB;
      using (MySqlConnection connectionCentral = new MySqlConnection(myConnStringCentral))
      {
         connectionCentral.Open();
      }
      string myConnStringCentralCopy = "User Id=*****;Password=*****;Host=" + globalSettings.settingCentralCopyIP + ";Database=" + globalSettings.settingCentralCopyDB;
      using (MySqlConnection connectionCentralCopy = new MySqlConnection(myConnStringCentralCopy))
      {
         connectionCentralCopy.Open();
      }
      transScope.Complete();
      Console.WriteLine("Transaction is completed");
   }
   catch (Exception)
   {
      Console.WriteLine("Transaction is rolled back");
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top