gpt4 book ai didi

c# - 操作对于 LINQ to SQL ExecuteCommand 事务的状态无效

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:45 25 4
gpt4 key购买 nike

所以我有以下代码:

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 个表。这是我得到异常的部分。 (在执行命令中)

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

我在并发设置为多个的情况下处理超过数十万个项目。

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

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

编辑:堆栈跟踪:

at System.Transactions.TransactionState.EnlistPromotableSinglePhase(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Transaction atomicTransaction)
at System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification) at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx) at System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions
userOptions, DbConnectionInternal& connection) at
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection
owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry) at System.Data.SqlClient.SqlConnection.Open() at System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser user) at System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe() at System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode() at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) at System.Data.Linq.DataContext.ExecuteCommand(String command, Object[] parameters)

最佳答案

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

添加“池=假;”到你的连接字符串

关于c# - 操作对于 LINQ to SQL ExecuteCommand 事务的状态无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21019552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com