.net 事务超时时间设置

写了一段程序,使用.net framework里的事务,设置了超时时间为30分钟,但是奇怪的是在10分钟的时候就报错了。搜索了一下,原来还需要修改machine.config(默认应该在C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG中),比如设置为30分钟:

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

 

需要注意的是,system.transactions应该放在configSections节点之后,否则程序启动时会报“无法识别的配置节 system.transactions”。

 

引用原文:


This came up a couple of times recently, so I am posting it more broadly. Courtesy of Mike Clark and Jim Carley. 

System.Transactions has two timeout values that you can specify in configuration files. 

The default timeout for System.Transactions transactions is 1 minute.  You can set it in either app config, web config, or machine config.  (You can also set the timeout for a particular transaction programmatically within the application, by using one of the appropriate constructors forTransactionScope or CommittableTransaction).  Setting the default timeout to 30 seconds in config code looks like the following.

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

For System.Transactions there is also a maximum transaction timeout. It is designed to be used by the System Administrator to limit transaction timeouts. If this setting is not specified, it defaults to 10 minutes.  It cannot be overridden in code.   If the app.config timeout or the timeout specified in the constructors above exceed the maximum timeout in the machine.config, the timeout is adjusted down to the maximum timeout value. That can be specified only in machine config.  To change that you would specify the maxTimeout property of the machine settings section.  For example, this specifies 30 seconds:

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

So, for example if your app.config setting specifies a defaultSettings timeout of zero, which implies (with a screwy sort of logic) infinite timeout, or if your application code specifies a zero timeout in one of the constructors, then the actual timeout of the transaction will not be infinite - it will be bound to the setting for machineSettings maxTimeout. 

For any high-throughput transactional server, The default maxTimeout setting is probably not right for you.  You're going to want to set this pretty low. 

This is for any transaction managed by DTC - that would include transactions involving SQL Server, Oracle, MQ, DB2, and so on.  If your transactions are timing out after 10 minutes and you want to know why, check these settings. 


———————————————— 
原文链接:https://blog.csdn.net/lazyleland/article/details/7988622

posted @ 2019-12-10 10:49  *人丑就该多读书*  阅读(777)  评论(0编辑  收藏  举报