14

Is there any way to globally setup time you would wait for connecting to a given database, before a connection failure in NHibernate (connection timeout)? In ADO.NET you can do it for a single connection like this:

new SqlConnection().ConnectionTimeout = 10;

I found how to setup the time you would wait for a result set, before a command execution failure here (command timeout). But, apparently, that's not what I need

Community
  • 1
  • 1
Igor Bendrup
  • 2,637
  • 2
  • 16
  • 15

3 Answers3

20

You can use the connection_timeout setting in your NHibernate configuration code. See section 3.4 of the documentation for full details.

The XML configuration for this is as follows...

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
        <property name="connection.connection_string">
            Server=(local);initial catalog=theDb;Integrated Security=SSPI
        </property>
        <property name="connection_timeout">100</property>
    </session-factory>
</hibernate-configuration>

<!-- other app specific config follows -->

I'm using Fluent NHibernate so my configuration code is as follows...

FluentConfiguration configuration = Fluently.Configure()
                         .Database(MsSqlConfiguration.MsSql2012.ConnectionString(ConnectionString))
                         .ExposeConfiguration(cfg => cfg
                            .SetProperty("connection_timeout", "100")
                         .Mappings(m =>
                         {
                             var cfg = CreateAutomappings();
                             m.AutoMappings.Add(cfg);
                         });
amcdermott
  • 1,565
  • 15
  • 23
  • 3
    There is a difference between a _command timeout_ and a _connection timeout_. Please refer [here](https://nirajrules.wordpress.com/2012/02/08/connectiontimeout-vs-commandtimeout/). The code you've provided, sets the command timeout, but I need a way to set the connection timeout – Igor Bendrup Jul 06 '15 at 09:27
  • Connection Timeout != Command Timeout i.e. connection timeout is the time of waiting connection available but command timeout is the max command execution time – Alexander Egorov Feb 15 '17 at 15:28
  • 1
    as others have stated, we are looking for connection timeout, not command timeout. your answer is misleading and can cause someone to change the wrong setting, that's why i down-voted. – neoscribe Jun 06 '18 at 20:13
  • Yes, in answering I originally typed command not connection - an easily made mistake I guess. Didn't bother editing as I felt somebody experienced enough to be using NHibernate would figure out the *connection_timeout* setting lives alongside its command equivalent. However, some people seem disproportionately vexed - even 5 years later - so, for their benefit, I've finally updated it. – amcdermott Nov 02 '20 at 15:26
  • There is no such setting as `connection_timeout`. – Chet Dec 30 '20 at 20:25
12

You can set it on the connection string, "Connection Timeout=x".

Roger
  • 1,944
  • 1
  • 11
  • 17
  • It is the simpliest way to solve the problem in my case. In some other cases may be better to write a [custom connection provider](https://davidhogue.com/blog/2012/02/plugging-in-a-connectionprovider-to-nhibernate-to-connect-to-multiple-databases/) and programatically set IDbConnection.ConnectionTimeout property – Igor Bendrup Jul 06 '15 at 14:57
  • 5
    Note that 'x' here is time expressed in seconds. – Tyler Forsythe Sep 08 '16 at 15:59
  • Isn't this property to specify the time to successfully make a connection not for the time to run queries? – 4imble Feb 15 '23 at 10:32
1

With NHibernate you can provide the connection yourself:

sessionFactory.openSession(myConnection);

I wouldn't recommend it, because it's easier when sessions are managed by NHibernate.

You can still write your own connection provider, which sets whatever you want on the created connections.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193