我正在尝试此代码,我认为该代码不应提升/提升TransactionScope到分布式事务(MSDTC),因为我读到某处MSDTC仅在两种情况下发生, 1.当我们有一个嵌套的TransactionScope 2.如果我们尝试在同一TransactionScope中打开2个同时连接到2个不同源的
但是我要关闭较早的连接,然后再打开到其他源的新连接,但是它仍在将事务提升为分布式的。
我得到的错误是“合作伙伴事务管理器已禁用对远程/网络事务的支持。”
以下是代码:
var options = new TransactionOptions()
{
IsolationLevel = System.Transactions.IsolationLevel.RepeatableRead,
Timeout = TimeSpan.FromMinutes(5)
};
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
{
// Connection To Server Database
string connectionString = "Server=188.188.188.188;Database=SomeDb;User=SomeUser;Password=SomePassword;Trusted_Connection=false;Connection Timeout=30;Persist Security Info=False";
SqlConnection conn1 = null;
try
{
conn1 = new SqlConnection(connectionString);
conn1.Open();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
if (conn1 != null)
{
conn1.Close();
conn1.Dispose();
conn1 = null;
}
}
// Connection To Local Machine Database
string connectionString2 = "Server=MyPC\\SqlServer2014;Database=SomeDb;User=SomeUser;Password=SomePassword;Trusted_Connection=false;Connection Timeout=30;Persist Security Info=False";
SqlConnection conn2 = null;
try
{
conn2 = new SqlConnection(connectionString2);
conn2.Open();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
conn2.Close();
conn2.Dispose();
conn2 = null;
}
scope.Complete();
}