默认事务超时 [英] Default Transaction Timeout

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

问题描述

我曾经使用 TransactionOptions.Timeout 设置事务超时,但决定使用config方法进行维护:

I used to set Transaction timeouts by using TransactionOptions.Timeout, but have decided for ease of maintenance to use the config approach:

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

当然,放入后,我想测试它是否工作, 5秒,然后运行测试持续时间比这更长 - 但事务似乎没有中止!如果我调整测试将TransactionOptions.Timeout设置为5秒,测试工作正常

Of course, after putting this in, I wanted to test it was working, so reduced the timeout to 5 seconds, then ran a test that lasted longer than this - but the transaction does not appear to abort! If I adjust the test to set TransactionOptions.Timeout to 5 seconds, the test works as expected

调查后我认为问题似乎与TransactionOptions.Timeout相关,即使我不再使用它。

After Investigating I think the problem appears to relate to TransactionOptions.Timeout, even though I'm no longer using it.

我仍然需要使用TransactionOptions,所以我可以设置IsolationLevel,但我不再设置Timeout值,如果我看看这个对象在创建它之后,超时值为00:00:00,等于无穷大。这是否意味着我在配置文件中设置的值被忽略?

I still need to use TransactionOptions so I can set IsolationLevel, but I no longer set the Timeout value, if I look at this object after I create it, the timeout value is 00:00:00, which equates to infinity. Does this mean my value set in the config file is being ignored?

总结:


  • 是不可能混合config
    设置和使用
    TransactionOptions

  • 如果没有,是否有
    以任何方式提取config在运行时设置
    ,并使用它来设置
    超时属性

  • [Edit] OR 使用TransactionOptions

  • Is it impossible to mix the config setting, and use of TransactionOptions
  • If not, is there any way to extract the config setting at runtime, and use this to set the Timeout property
  • OR Set the default isolation-level without using TransactionOptions

推荐答案

您可以混合使用system.transaction配置设置和< c $ c> TransactionOption 类,但有一些你需要注意的事情。

You can mix system.transaction configuration settings and the use of the TransactionOption class, but there are some things you need to be aware of.


使用 TransactionOption
指定超时值,该值
将用于
system.transactions / defaultTimeout
value。

If you use the TransactionOption and specify a Timeout value, that value will be used over the system.transactions/defaultTimeout value.

上面是问题的症结所在认为。您正在使用 TransactionOption 指定隔离级别,作为副作用,您将获得无限超时值因为infinite是 TransactionOption 的默认超时值(如果未指定)。虽然,我不太确定为什么这是...默认的默认事务超时是有意义的。

The above is the crux of the problem in your case I think. You are using the TransactionOption to specify the isolation level, and as a side effect you are getting an infinite Timeout value because infinite is the default Timeout value for TransactionOption if its not specified. Though, I'm not quite sure why that is...it would make sense to default to the default Transaction Timeout.

你可以实现自己的TransactionOptions帮助类包括从app.config中读取的默认值(如果找到)或默认值为可以使用的TransactionOption类的合理值。

You can implement your own TransactionOptions helper class that includes defaults that are read from app.config (if found) or default to reasonable values for a TransactionOption class that can be used.

在任何情况下,通过使用 system.transaction / machineSettings / maxTimeout 值。这是一个管理设置,只能通过machine.config配置。

In any case, you can still limit this by using the system.transaction/machineSettings/maxTimeout value. This is an administrative setting and can only be configured through the machine.config. You'll get a ConfigurationException if you try it from app/web.config.

<system.transactions>
    <machineSettings maxTimeout="00:00:30" />
</system.transactions>

使用 maxTimeout 设置,无论您指定什么超时值,最大值将限制为maxTimeout值。默认的maxTimeout是00:10:00,或10分钟,所以你实际上不会有一个事务的无限超时。

With the maxTimeout set, no matter what timeout value you specify, the maximum value will be limited to the maxTimeout value. The default maxTimeout is 00:10:00, or 10 minutes, so you wouldn't actually ever have an infinite timeout on a transaction.

你也可以设置事务IsolationLevel 显式地对您在事务中使用的数据库连接。这样的东西吗?

You can also set the transaction IsolationLevel explicitly on the database connection you are using within the transaction. Something like this?

   var connectionString = "Server=.;Database=master;Trusted_Connection=True;";

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    var sqlTransaction = conn.BeginTransaction(System.Data.IsolationLevel.Serializable);

                    // do database work
                    //
                    sqlTransaction.Commit();


                }

                // do other work..
                //

                scope.Complete();

            }

在测试中,您可能需要确保重建以便重新生成app.config。在我的测试中,似乎我需要终止* .vshost.exe进程,以便它拿起system.transaction配置设置更改 - 虽然我觉得可能是一个侥幸。只是fyi ..

In your testing, you may need to make sure you rebuild so that the app.config is regenerated . In my testing, it appeared that I needed to terminate the *.vshost.exe process in order for it to pick up the system.transaction configuration setting change - though I feel that may have been a fluke. Just fyi..

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

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