locked
Set Command timeout in NHibernate RRS feed

  • Question

  • User908929258 posted

    Can we also have session level timeouts ?

    Please help me to Set Command timeout in NHibernate

    Code is using getting NHibernate configuration and using BuildSessionFactory, OpenSession methods.

    Monday, November 27, 2017 10:44 PM

All replies

  • User-1838255255 posted

    Hi imcoolcoolmi,

    According to your description, about how to set the timeout of the command in NHibernate, please check the following tutorial:

    Chapter 3. ISessionFactory Configuration:

    http://nhibernate.info/doc/nhibernate-reference/session-configuration.html 

    How to set timeout for NHibernate LINQ statement:

    https://stackoverflow.com/questions/12919361/how-to-set-timeout-for-nhibernate-linq-statement 

    Best Regards,

    Eric Du

    Tuesday, November 28, 2017 2:52 AM
  • User991499041 posted

    Hi imcoolcoolmi,

    Can we also have session level timeouts ?

    Please help me to Set Command timeout in NHibernate

    Code is using getting NHibernate configuration and using BuildSessionFactory, OpenSession methods.

    Welcome to ASP.NET Forums.

    You have multiple options to change the timeout value.

    Global change

    If you want to change it for all queries in your application, you can configure the following value in your NHibernate configuration:

    command_timeout: Specify the default timeout of IDbCommands generated by NHibernate.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="hibernate-configuration"
            type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
      </configSections>
      <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.isolation">ReadCommitted</property>    
          <property name="command_timeout">100</property>
        </session-factory>
      </hibernate-configuration>
      <!-- other app specific config follows -->
    </configuration>

    Local change

    If you only want to change this for a specific query, you can set it using the SetTimeout method 

    IList<Cat> cats =
        session.Query<Cat>()
            .Where(c => c.Color == "black")
            // Allows 10 seconds only.
            .SetOptions(o => o.SetTimeout(10))
            .ToList();

    Regards,

    zxj

    Thursday, November 30, 2017 7:01 AM