【问题标题】:Operation is not valid for the state of the transaction LINQ to SQL ExecuteCommand操作对于事务 LINQ to SQL ExecuteCommand 的状态无效
【发布时间】:2014-01-09 11:56:00
【问题描述】:

所以我有以下代码:

var transactionScopeOptions = new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted, Timeout = TimeSpan.FromMinutes(2) };
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionScopeOptions))
            {

/* Linq update, insert, delete, read, etc. */


protected const string UpdateQuery = @"SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
                             BEGIN TRANSACTION;

                             /* dirty reads & 3 table updates */

                             COMMIT TRANSACTION;"

   dataContext.ExecuteCommand(UpdateQuery, new object[] { fooID, fooTime })
   transactionScope.Complete();
            }

一开始,我不做 Linq to Sql 字符串查询,然后在一个类中,我使用了一个很长的字符串查询来更新 3 个表。这是我得到异常的部分。 (在 ExecuteCommand 中)

环境是WCF多线程,一个项目开始的时间有一个日志,项目结束的时候有另一个日志,不是超时异常。

我处理了数十万个并发设置为多个的项目。

知道为什么会抛出异常吗? dataContext 每个项目仅实例化一次。

编辑: 命令超时设置为 1 小时,错误在开始后几分钟内发生。如果队列中的项目少于 100,000 个,通常不会发生这种情况。

编辑:堆栈跟踪:

在 System.Transactions.TransactionState.EnlistPromotableSinglePhase(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Transaction atomicTransaction)
在 System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification)在 System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(事务 TX)在 System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(事务 交易)在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean 允许创建、布尔型仅OneCheckConnection、DbConnectionOptions userOptions,DbConnectionInternal& 连接)在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 重试,DbConnectionOptions userOptions,DbConnectionInternal& 连接)在 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection 外部连接,DbConnectionFactory 连接工厂, 任务完成源1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 重试)在 System.Data.SqlClient.SqlConnection.Open() 在 System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser 用户)在 System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe() 在 System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode() 在 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(表达式 查询)在 System.Data.Linq.DataContext.ExecuteCommand(字符串 命令,Object[] 参数)

【问题讨论】:

  • 你能添加堆栈跟踪吗?
  • 那里,刚刚添加了堆栈跟踪..

标签: c# multithreading wcf transactionscope


【解决方案1】:

您可以尝试在连接字符串中关闭连接池,我注意到在 SQL 操作通过 IIS 登记的分布式事务提交或回滚之前,连接似乎已返回到池中。虽然这种情况的症状通常是连接超时,但可能值得一试。

添加“pooling=false;”到你的连接字符串

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2010-09-16
    • 2011-08-24
    • 2011-09-30
    • 2011-04-24
    相关资源
    最近更新 更多