的TransactionScope超时过早发生? [英] TransactionScope Timeout occurs prematurely?

查看:1675
本文介绍了的TransactionScope超时过早发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的TransactionScope 做一些批量插入和更新。问题是,我在一个30分钟长的操作变得超时异常甚至当我设置了的TransactionScope 一小时的超时时间。



另外,根据异常后它插入的批次的记录看似随机量。例如最近的操作有12440插入和超时后,有7673条记录插入到表中。



的超时的SqlConnection 的SqlCommand 均设置为 int.MaxValue



我在做什么错



下面是我的代码:

 使用(TransactionScope的交易=新的TransactionScope(TransactionScopeOption.Required,TimeSpan.FromHours(1)))
{

{
使用(DB =新的DB( ))
{
//这里的操作
}
}
赶上(异常前)
{
抛出新的异常(DB错误:\r\\\
\r\\\
+ ex.Message);
}

transaction.Complete();
} //< ---异常这里:事务终止(内部异常:超时)


解决方案

是您的交易10分钟后失败?如果是这样,你很可能击中事务管理器最大超时这是在machine.config设置。如果我没有记错,如果你尝试设置超时时间超过最大值那么你的设置将被忽略更大。尝试正在增加在machine.config中值,看看是否能帮助您的问题。



在随机提交方面你设置交易绑定=显式取消绑定您的连接字符串?默认值为交易绑定=隐拆散。从 MSDN




隐取消绑定导致连接
从当
结束事务分离。分离后,连接上的额外
请求是在自动提交模式下执行
。执行
请求时,而交易
激活时不检查
System.Transactions.Transaction.Current
属性。成交后有
结束,其他请求都自动提交模式下执行




基本上,当交易超时所有插入到这一点,将被回退,但使用相同的连接做任何额外的刀片将在自动提交模式下,每一个插入语句将被立即提交完成。这听起来像您所看到的情况(但它很难知道没有看到完整的代码/摄制)。


I'm using TransactionScope to do some batch insert and updates. Problem is, I'm getting timeout exceptions on a 30 min long operation even when I set the timeout of the TransactionScope to one hour.

Also after the exception it inserts seemingly random amount of the records of the batch. For example the last operation had 12440 inserts and after the timeout there were 7673 records inserted to the table.

The timeout of the SqlConnection and SqlCommand are both set to int.MaxValue.

What am I doing wrong?

Here's my code:

using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromHours(1)))
 {
         try
         {
                using (db = new DB())
                {
                //operations here
                }
         }
         catch (Exception ex)
         {
               throw new Exception("DB Error:\r\n\r\n" + ex.Message);
         }

         transaction.Complete();
} // <--- Exception here: Transaction aborted (Inner exception: Timeout)

解决方案

Is your transaction failing after 10 minutes? If so, you are probably hitting the Transaction Manager Maximum Timeout which is set in the machine.config. If I recall correctly, if you try to set a timeout greater than the maximum value then your setting will be ignored. Try upping the value in machine.config and see if that helps your issue.

In terms of random commits do you set Transaction Binding=Explicit Unbind on your connection string? The default value is Transaction Binding=Implicit Unbind. From MSDN:

Implicit Unbind causes the connection to detach from the transaction when it ends. After detaching, additional requests on the connection are performed in autocommit mode. The System.Transactions.Transaction.Current property is not checked when executing requests while the transaction is active. After the transaction has ended, additional requests are performed in autocommit mode.

Basically, when the transaction times out all inserts up to that point will be rolled back but any additional inserts done using the same connection will be done in autocommit mode where every insert statement will be immediately committed. That does sound similar to the scenario you are seeing (but it's hard to know without seeing the full code/repro).

这篇关于的TransactionScope超时过早发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆