我有以下代码:
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. }
更多推荐
发布评论