了解transactionscope超时 [英] Understanding transactionscope timeouts

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

问题描述

我目前对transactionscope超时的理解.

My current understanding of transactionscope timeouts.

如果事务的运行时间超过了设置的超时时间,则在以下情况下会引发异常:transaction.complete()被调用.因此,如果交易中的处理一直在进行在X分钟内,我们仍然需要等待X分钟,之后才会调用transaction.complete.

If a transaction has been running longer than the set timeout time it throws an exception when transaction.complete() is called. So if the processing within the transaction has been going on for X minutes we still would have to wait X minutes after which the transaction.complete is called.

在我们的示例中,我们从Web服务内部使用transactionscope-Web请求的最终用户在事务中止和异常冒泡之前,您将需要等待X分钟.

In our case, we are using transactionscope from within a webservice - the end user of the web request will have to wait for X minutes before the transaction is aborted and the exception bubbled back.

但是,HttpWebRequest的默认超时为100秒(根据msdn).自客户时代以来在100秒内,我们在transactionscope中的超时时间为一分钟.这样可以确保数据库的一致性.

However, the default timeout for a HttpWebRequest is 100 seconds (according to msdn). Since the client times out in 100 seconds we have a timeout in transactionscope of a minute. This ensures database consistency.

我对超时的理解正确吗?

Is my understanding of timeouts correct?

问题:我们希望最大程度地减少最终用户了解交易结果所花费的时间.为了最大程度地减少延迟,我们决定使用嵌套事务范围拆分代码-每个范围的超时时间为15秒.如果子事务花费的时间超过15秒,我们将整体上中止该事务.

Question: We would like to minimize the time it takes for the end user to know the results of the transaction. To minimize latency we decided to split up the code using nested transactionscopes - each with a timeout of say 15 seconds. If a child transaction is taking longer than a 15 seconds we abort the transaction as a whole.

在这里,似乎子事务的超时被忽略了.我只有在调用父事务的超时.在以下代码中,ChildTransaction()始终返回true.建议采用什么方法来最大程度地减少延迟?代码显示默认超时为1分钟,只是为了使代码更简洁

Here it seems that the child transaction's timeout is ignored. I get an exception only after the parent transaction's timeout is called. In the following code ChildTransaction() always returns true. What is the recommended approach to minimize the latency? The code shows default timeout of 1 minute just so the code is cleaner

    internal bool RootTransaction()
    {           
        using (TransactionScope transaction = new TransactionScope())
        {
            try
            {
                bool result = ChildTransaction();

                //The result is always true. 
                if (!result)
                    return result;                                     

                for (int counter = 0;counter <= 10;counter++)
                {                       
                    //Either sleep OR do some processing 
                    System.Threading.Thread.Sleep(5000);
                    //
                    //Dosomeprocess()
                }                    
                transaction.Complete();                    
                return true;
            }
            catch (Exception e)
            {                    
                return false;
            }
        }                           
    }

    internal bool ChildTransaction()
    {            
        using (TransactionScope transaction = new TransactionScope())
        {
            try
            {
    //Sleep for 70 seconds
                System.Threading.Thread.Sleep(70000);
                transaction.Complete();                    
            }                
            catch (Exception e)
            {
                return false;
            }                
        }
        return true;
    }

推荐答案

尝试以这种方式查看它:

Try to look at it this way:

仅当您调用trans.Complete()或退出事务范围时,才确定事务的长度.输入以下代码:

The length of the transaction is only determined when you call trans.Complete() or exit the transaction scope. Take the following code:

using (var trans= new TransactionScope())
{
Threading.Sleep(99999);
trans.Complete()
}

在睡眠例程中无法抛出超时异常,如果这样做,则没有任何意义.因此,使用事务超时(至少是这种方式)只能保证,如果事务花费的时间超过您的超时时间,则不会提交该事务.

There is no way to throw a timeout exception while inside the sleep routine and it wouldn't make sense if it did. And so using transaction timeouts (this way at least) can only guarantee that if the transaction takes longer than your timeout, it will not be commited.

如果您仅执行一个查询(我不知道您将事务用于什么),则可以设置查询/命令超时(或任何您称呼的超时).IIRC,您的查询将在超时到期后立即返回.

If you are just executing one query (which I wouln't know what you use the transactions for) then you could set the query/command timeout (or whatever you call it). IIRC, your query will return immediately after the timeout expires.

另一种方法是设置您的Web服务请求超时,并仅假定由于事务内部存在任何原因,Web服务花费的时间太长.

Another way would be to set your web service request timeout and just assume that the webservice is taking too long to respond because of whatever was inside your transaction.

您可以尝试:

  • 在另一个线程上生成事务,然后等待它在主线程(由webservice调用使用的线程)上完成(使用Thread.Join(timeout)).因此,如果它没有在您指定的超时之前终止,那么您可以停止等待并返回超时错误(不要忘记发信号通知另一个线程中止事务).
  • 假设您仅在这些事务内执行SQL查询,则可以使用"BEGIN TRANSACTION"关键字在sql脚本中指定事务(实际上是hacky).然后,您只需指定命令超时,然后在一行代码中执行所有这些操作即可.但这然后需要您将在事务中执行的所有操作都移至可能无法执行的sql脚本中……而且这并不干净.

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

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