c# TransactionScope time out

本文关键字:out time TransactionScope | 更新日期: 2023-09-27 18:10:24

在我的c代码中,我使用了一个事务作用域

但是在10min后给出超时异常,如下;

System.Transactions.TransactionAbortedException: The transaction has aborted. ---> System.TimeoutException: Transaction Timeout
   --- End of inner exception stack trace ---
   at System.Transactions.TransactionStateAborted.BeginCommit(InternalTransaction tx, Boolean asyncCommit, AsyncCallback asyncCallback, Object asyncState)
   at System.Transactions.CommittableTransaction.Commit()
   at System.Transactions.TransactionScope.InternalDispose()
   at System.Transactions.TransactionScope.Dispose()

我有以下配置在我的网页。配置文件;

  <system.transactions>
    <defaultSettings timeout="22:00:00" />
  </system.transactions>

为什么我仍然得到这个超时?如何解决这个问题?

c# TransactionScope time out

您应该能够通过确保transactionscope的超时时间比命令超时时间长来消除它。

尝试将事务范围更改为如下内容:

new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromSeconds(60)) //you already defined in web config  so it's fine

如果您正在使用实体连接到数据库,请在上下文上设置显式超时。应该是这样的:

myContext.CommandTimeout = 30; //This is seconds

检查%windir%'Microsoft.NET'Framework64'v4.0.30319'Config'machine.config文件中的machineSettings/maxTimeout值(如果使用32位CLR或其他版本,可能需要更改此路径)。

此值限制了有效超时,您设置的每个超时(通过defaultSettings或通过TransactionScopeOptions在代码中)将被限制为此值。"默认值"为10分钟。

你可以在网上搜索"machinessettings maxTimeout",找到一些相关的博客条目