首次打开EF4,.Net4.5,SQL Server 2012的MSDTC升级(MSDTC escalation on first open EF4, .Net4.5, SQL Server 2012

编程入门 行业动态 更新时间:2024-10-25 21:27:37
首次打开EF4,.Net4.5,SQL Server 2012的MSDTC升级(MSDTC escalation on first open EF4, .Net4.5, SQL Server 2012)

我有以下代码:

var scopeOption = TransactionScopeOption.RequiresNew;
var transactionOptions = new TransactionOptions
{
    IsolationLevel = IsolationLevel.ReadUncommitted
};
using (new TransactionScope(scopeOption, transactionOptions))
{
    var context = new ObjectContext("connection string");
    context.Connection.Open(); // <-- This line throws
}
 

它抛出以下异常:

System.Data.EntityException: The underlying provider failed on Open.
Caused by: System.Data.SqlClient.SqlException, MSDTC on server 'xxxx' is unavailable.
 

但它只会在第一次尝试打开连接时抛出异常。 对示例代码的任何后续调用都可以正常工作,并且不会尝试联系DTC。

注意:将scopeOption更改为TransactionScopeOption.Suppress似乎可以解决问题,因为我没有对数据库执行任何写操作,这是可以接受的。

任何人都可以想到为什么在全新的TransactionScope打开第一个(也是唯一的)连接只会在第一次调用代码时导致DTC升级?

I have the following code:

var scopeOption = TransactionScopeOption.RequiresNew;
var transactionOptions = new TransactionOptions
{
    IsolationLevel = IsolationLevel.ReadUncommitted
};
using (new TransactionScope(scopeOption, transactionOptions))
{
    var context = new ObjectContext("connection string");
    context.Connection.Open(); // <-- This line throws
}
 

It throws the following exception:

System.Data.EntityException: The underlying provider failed on Open.
Caused by: System.Data.SqlClient.SqlException, MSDTC on server 'xxxx' is unavailable.
 

But it only throws the exception the on the first attempt to open the connection. Any subsequent calls to the example code work perfectly and don't try to contact the DTC.

Note: Changing scopeOption to TransactionScopeOption.Suppress seems to fix the issue, and as I'm not performing any writes to the database is more than acceptable.

Can anybody think of why opening the first (and only) connection in a brand-new TransactionScope would cause a DTC escalation only the first time the code is invoked?

最满意答案

如果有人遇到过这个问题,我实现这个问题的唯一方法就是更改代码,以便在事务范围之外创建上下文。

var scopeOption ...
var transactionOptions ...

var context = new ObjectContext("connection string");
using (new TransactionScope(scopeOption, transactionOptions))
{
    context.OpenConnection();
    //do other stuff here with the context.
}

In case anybody ever has this issue, the only way I got this to work was to change the code so the creation of the context happens outside the transaction scope.

var scopeOption ...
var transactionOptions ...

var context = new ObjectContext("connection string");
using (new TransactionScope(scopeOption, transactionOptions))
{
    context.OpenConnection();
    //do other stuff here with the context.
}

                    
                     
          

更多推荐

本文发布于:2023-08-06 10:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:首次   SQL   Server   MSDTC   escalation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!