3

In my C# .NET 3.5 application I am using CastleProject ActiveRecord over NHibernate. This is desktop application using MS SQL Server 2008. I have set ADO command timeout to 0 to prevent timeout exception during bulk operations:

  <activerecord>
    <config>
      ...
      <add key="hibernate.command_timeout" value="0" />
    </config>
  </activerecord>

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      ...
      <property name="command_timeout">0</property>
    </session-factory>
  </hibernate-configuration>

However, I am still receiving timeout exception! The NHibernate log shows something like this:

Somewhere at the beginning:

2010-10-02 06:29:47,746 INFO NHibernate.Driver.DriverBase - setting ADO.NET command timeout to 0 seconds

Somewhere at the end:

2010-10-02 07:36:03,020 DEBUG NHibernate.AdoNet.AbstractBatcher - Closed IDbCommand, open IDbCommand s: 0 2010-10-02 07:36:03,382 ERROR NHibernate.Event.Default.AbstractFlushingEventListener - Could not syn chronize database state with session NHibernate.HibernateException: An exception occurred when executing batch queries ---> System.Data.S qlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the opera tion or the server is not responding. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

How come? How to fix this?

Alex
  • 1,357
  • 3
  • 18
  • 41
  • this a web application I'm assuming? – hardba11 Oct 02 '10 at 04:56
  • Sorry, did not state this. No, this is regular desktop application. – Alex Oct 02 '10 at 05:21
  • Are you using batching (`adonet.batch_size` > 0) ? Why do you have an ActiveRecord configuration *and* a separate NHibernate configuration? What kind of SQL statement is timing out (update/insert/select/delete)? What version of NHibernate and ActiveRecord are you using? – Mauricio Scheffer Oct 02 '10 at 16:23
  • @Mauricio: Yes, I am using batch_size=1000. I have two configurations because in the past I met situations when one key works being defined in ActiveRecord section while another works only for Nhibernate so I decided to have both (maybe this is wrong). The statement is insert, batch insert. Nhibernate is 2.1.2.4000 and CastleProject is 2.1.0.6692. – Alex Oct 02 '10 at 19:26
  • I have a theory... try setting batch_size to 0, and see if it still times out. Please post back here with the result. – Mauricio Scheffer Oct 02 '10 at 20:01
  • @Mauricio, you are right, it does not fail with batch_size equal to 0. What is the theory? – Alex Oct 03 '10 at 04:43
  • BTW normally you should not have two configurations... unless you know what you're doing ;-) If you're using ActiveRecord just use an ActiveRecord configuration. – Mauricio Scheffer Oct 04 '10 at 14:02
  • The proper timeout config key is `command_timeout`, not `hibernate.command_timeout` (which IIRC was renamed in NH 2.0) – Mauricio Scheffer Oct 04 '10 at 14:03
  • Will try with "command_timeout". Strange that ActiveRecord did not fail like it does when meet unknown configuration options. – Alex Oct 04 '10 at 14:24
  • I deleted all timeouts/batch-sizes from ActiveRecord section. However, it won't run in case I remove hibernate.dialect and hibernate.connection.provider, so I left them. All other options, including command_timeout and adonet.batch_size I left only in hibernate-configuration. I am still receiving exception about disconnected transaction (which is described in the new issue I opened). – Alex Oct 04 '10 at 16:29
  • @Alex: the "hibernate." prefix was removed in NH 2.0 – Mauricio Scheffer Oct 07 '10 at 03:33

2 Answers2

5

It's correct that a value of 0 indicates no timeout (as defined in the MSDN docs), however while NHibernate's driver passes the config value to the db command when it's >= 0, the batcher's condition checks that the value is > 0.

Therefore, when you set batching on, with a timeout value of 0, the value isn't carried over to the db command so it remains as default.

It's entirely possible that this is by design, and that NHibernate developers intentionally disabled disabling timeouts for batch scenarios. Disabling timeout is a bad idea anyway, if you have troubles with timeout errors I would raise the value, but not disable it.

Please confirm this with NHibernate devs.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • I tried to increase timeout time (from 1000 to 10000) and decreased batch size (from 1000 to 100), now I get exception NHibernate.TransactionException: Transaction not connected, or was disconnected on quite random place of my batch operations. Why? – Alex Oct 04 '10 at 06:08
  • Hm... I get timeout exception once more what is impossible for such long timeout value. One of my friends said me that Nhibernate TransactionScope I am using do not use custom timeout. Do you know any workaround to populate timeout from config file to TransactionScope? – Alex Oct 04 '10 at 07:42
  • @Alex: please post another question. – Mauricio Scheffer Oct 04 '10 at 11:44
  • Posted: http://stackoverflow.com/questions/3855315/timeout-exception-when-using-nhibernate-transactionscope – Alex Oct 04 '10 at 12:44
  • How to set batcher connection timeout through fluent nhibernate? – Johnny_D Apr 26 '12 at 07:33
  • @Johnny_D : please post a new question. – Mauricio Scheffer Apr 26 '12 at 12:56
0

You might be looking to set the timeout for specific queries and not at web.config level (otherwise you really need to tune up your application :) ).

I've recently found this answer that helped me:

How to set Nhibernate LINQ Command Timeout using Session.Query

Community
  • 1
  • 1
edumen
  • 171
  • 1
  • 1
  • 11