默认事务超时
我曾经使用 TransactionOptions.Timeout 设置事务超时,但为了便于维护,我决定使用配置方法:
<system.transactions>
<defaultSettings timeout="00:01:00" />
</system.transactions>
当然,放入此后,我想测试它是否正常工作,因此减少了将超时设置为 5 秒,然后运行比这更长的测试 - 但事务似乎并未中止!如果我调整测试以将 TransactionOptions.Timeout 设置为 5 秒,则测试将按预期工作。
调查后,我认为问题似乎与 TransactionOptions.Timeout 有关,即使我不再使用它。
我仍然需要使用 TransactionOptions 以便设置 IsolationLevel,但我不再设置 Timeout 值,如果我在创建该对象后查看该对象,超时值为 00:00:00,相当于无穷大。这是否意味着我在配置文件中设置的值被忽略?
总结一下:
- 是否不可能混合配置 设置和使用 TransactionOptions
- 如果没有,有吗 任何提取配置设置的方法 在运行时,并使用它来设置 超时属性
- [编辑]或设置默认隔离级别而不使用TransactionOptions
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>
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
After Investigating I think the problem appears to relate to TransactionOptions.Timeout, even though I'm no longer using it.
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?
To summarise:
- 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 - [Edit] OR Set the default isolation-level without using TransactionOptions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 从配置中获取(经过验证的)默认超时
TransactionManager.DefaultTimeout
。TransactionOptions
是一个结构体封装超时和隔离级别。使用默认构造函数初始化 struct 时,它总是将结构成员初始化为其默认值:如果您想指定
IsolationLevel
并使用默认超时:You can get the (validated) default timeout from the configuration using
TransactionManager.DefaultTimeout
.TransactionOptions
is a struct that encapsulates timeout and isolation level. When initializing a struct using the default constructor, it will always initialize the struct members to their default values:If you want to specify an
IsolationLevel
and use the default timeout:您可以混合使用 system.transaction 配置设置和
TransactionOption
类,但有一些事情您需要注意。我认为以上是您案例中问题的症结所在。您使用
TransactionOption
指定隔离级别,作为副作用,您将获得无限超时值,因为无限是默认超时如果未指定,则为TransactionOption
的值。不过,我不太确定为什么会这样......默认为默认事务超时是有意义的。您可以实现自己的 TransactionOptions 帮助程序类,其中包括从 app.config 读取的默认值(如果找到)或默认为可使用的 TransactionOption 类的合理值。
在任何情况下,您仍然可以使用 system.transaction/machineSettings/maxTimeout 值来限制这一点。这是一项管理设置,只能通过 machine.config 进行配置。如果您从 app/web.config 尝试,您将收到 ConfigurationException。
设置了 maxTimeout 后,无论您指定什么超时值,最大值都将限制为 maxTimeout 值。默认 maxTimeout 为 00:10:00,即 10 分钟,因此事务实际上不会有无限超时。
您还可以在事务中使用的数据库连接上显式设置事务隔离级别。像这样的东西吗?
在测试中,您可能需要确保重建,以便重新生成 app.config 。在我的测试中,我似乎需要终止 *.vshost.exe 进程才能使其获取 system.transaction 配置设置更改 - 尽管我觉得这可能是侥幸。仅供参考..
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.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 forTransactionOption
if its not specified. Though, I'm not quite sure why that is...it would make sense to default to the default Transaction Timeout.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.
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.
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.
You can also set the transaction IsolationLevel explicitly on the database connection you are using within the transaction. Something like this?
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..
根据 Reflector,使用
TransactionScope
的构造函数设置事务超时的基本规则如下:DefaultTimeOut 由下面满足的第一个规则确定:
TimeSpan
参数,如果构造函数具有TransactionOption
参数,则 DefaultTimeout 是该参数,transactionOption.TimeOut
TransactionOption
参数,则 构造函数有一个TransactionScopeOption
参数,DefaultTimeout 是scopeOption.TimeOut
除非在 machine.config 中指定了另一个值,否则 MaxTimeOut 为 10 分钟。
事务的有效超时是 MaxTimeOut 和 DefaultTimeOut 中大于零的较小者。如果 MaxTimeOut 和 DefaultTimeOut 都为零,则有效超时是由
long.MaxValue
表示的刻度数(无穷大)。如果
TransactionScope
实例没有创建新事务,因为事务被传递到其构造函数中,或者因为事务范围选项不需要它(例如,当存在环境事务并且 TransactionScopeOption 为必填),但构造函数中仍传递一个timeOut
参数,启动计时器。当超时期限过去时,将调用底层事务的TimeOut()
方法。在这种情况下,不使用 DefaultTimeOut 和 MaxTimeOut 属性。如果
transactionScopeOption == TransactionScopeOption.Supress
,则超时将被忽略并且不起作用。如果覆盖 machine.config 中的相关部分,也可以在 app/web 配置文件中定义 MaxTimeOut(请注意allowDefintion 和allowExeDefinition 属性的值):
为了快速参考,以下是 TransactionScope 构造函数:
Per Reflector, the basic rules for setting a transaction time out with the constructors of
TransactionScope
are as follows:The DefaultTimeOut is determined by the first rule from below that is satisfied:
TimeSpan
parameter, the DefaultTimeout is the that parameterTransactionOption
parameter, the DefaultTimeout istransactionOption.TimeOut
TransactionScopeOption
parameter, the DefaultTimeout isscopeOption.TimeOut
The MaxTimeOut is 10 minutes unless another value is specified in the machine.config.
The effective timeout for the transaction is smaller of the MaxTimeOut and DefaultTimeOut that is greater than zero. If both MaxTimeOut and DefaultTimeOut are zero, the effective timeout is the number of ticks represented by
long.MaxValue
(the infinity).If the
TransactionScope
instance does not create a new transaction either because a transaction is passed into its constructor, or because the transaction scope option does not require it (e.g., when an ambient transaction is present and the TransactionScopeOption is Required), but atimeOut
parameter is still passed in the constructor, a timer is started. When the timeout period elapses, the underlying transaction'sTimeOut()
method is called. The DefaultTimeOut and MaxTimeOut properties are not used in this case.If the
transactionScopeOption == TransactionScopeOption.Supress
, the timeout is ignored and has no effect.It is also possible to define the MaxTimeOut in the app/web config file, if the relevant section in the machine.config is overridden (note the values of the allowDefintion and allowExeDefinition attributes):
For quick reference, here are the TransactionScope constructors:
使用 TransactionOptions 时,配置文件设置将被忽略。在大多数情况下,创建 TransactionScope 将创建 CommittableTransaction 的实例。 CommittableTransaction 的无参数构造函数将使用配置文件设置作为其默认超时。采用 TransactionOptions 或 TimeSpan 的 TransactionScope 构造函数将调用 CommittableTransaction 类的重载之一,而不是无参数版本。因此,如果您想使用该值,您必须自己从配置文件中获取它。
当我遇到这个问题时,我将以下代码放入一个小的 TransactionOptionsFactory 类中。
The config file setting is ignored when TransactionOptions are used. Creating a TransactionScope will, in most cases, create an instance of CommittableTransaction. The no arg constructor of CommittableTransaction will use the config file setting as its default timeout. TransactionScope constructors that take a TransactionOptions or TimeSpan will call one of the overloads of the CommittableTransaction class and not the no arg version. So if you want to use that value you have to grab it from the config file yourself.
When I ran into this I put the following code in a little TransactionOptionsFactory class.
写下我目前的想法:
To put my current thoughts down: