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