我们一直在使用带有.net连接器的C#和mysql,但是使用单独的提交,有时它无法全部提交。所以我们现在转向这个支持分布式事务的工具http://www.devart.com/dotconnect/mysql/。一切正常,只是我们不太确定如何进行多重连接。方法1将每个连接嵌套到另一个中。方法2是分开的。我们应该关闭连接或由transScope.Complete()处理;和transScope.Dispose();
方法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();
}
方法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();
}
答案 0 :(得分:1)
两种方法都是正确的。在两种情况下,TransactionScope将与dotConnect for MySQL一起使用。以下代码不是必需的:
else
transScope.Dispose();
因为退出using (TransactionScope transScope = new TransactionScope())
块时会自动调用Dispose方法。
...我完成了它意味着在我的情况下,当我打电话给完整的仪式时,transactionScope将关闭它...
不,transScope.Complete()不会关闭连接。如果在TransactionScope的使用块内关闭连接,则连接对象将被关闭,但是如果在transScope.Complete()之前调用transScope.Dispose(),则内部连接将保持打开状态以便向数据库发送更改。呼叫。如果为连接对象调用了Close或Dispose方法,则transScope.Dispose()将关闭内部连接。如果连接对象未关闭,则transScope.Dispose()不对连接执行任何操作。
...我的每个连接中的另一件事我跟踪try和catch,如果有任何错误我将rollbackBoolean标记为1,那么它就不会完成并且整个事务应该回滚是正确的机制? ......
如果发生错误,请不要调用完成。如果未调用Complete方法,则在执行Dispose方法时将回滚事务。
以下是带有try / catch块的示例,并在出现错误时回滚事务:
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");
}
}