How to set the timeout on a NHibernate transaction

2019-05-01 12:05发布

问题:

I need a lot of DB processing done in a single transaction, including some processing using NHibernate.

To make everything work in the same transaction, I'm using NHibernate's Session to start it, and enlist the the commands for the other work in it.

Everything goes OK until I commit. At that time I get a transaction timeout.

How can I set the NHibernate transaction timeout value sufficiently high?

We use FluentNHibernate.

回答1:

After some trial and lots of error I found this:

It appears that NHibernate takes the "TransactionManager.DefaultTimeout" value.

It can be set via

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

in the app/web config

If it is set to a value higher than TransactionManager.MaximumTimeout the transaction timeout will default to that. If you need longer you can lengthen that time by updating the "machine.config" for your .Net framework version with:

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