我们一直在使用带有 .net 连接器的 C# 和 mysql,但是在单独提交时,有时它无法全部提交。所以我们现在转向这个支持分布式事务的工具http://www.devart.com/dotconnect/mysql/ 。一切正常,只是我们不太确定如何放置多个连接。方法 1 是将每个连接嵌套到另一个中。方法2是分开的。我们应该关闭连接或由 transScope.Complete() 处理的位置;和 transScope.Dispose();
方法一。
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();
}
方法二
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();
}