title: Npgsql nhibernate 57014 canceling statement due to statement timeout
body: <p>I'm getting random errors in some queries using Npgsql.</p>
  
  <p>Here is the stack trace:</p>
  
  <pre><code>at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1597
     at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1497
     at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1487
     at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 1955
     at NHibernate.Impl.CriteriaImpl.List(IList results) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\CriteriaImpl.cs:line 265
     at NHibernate.Impl.CriteriaImpl.List[T]() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\CriteriaImpl.cs:line 276
     at LAVASPORT.DAO.MigrarCadeteDAO.findByCompania(Compania compania, String conexion, DateTime fechainicial, DateTime fechafinal) in D:\Aplicaciones\LavaSport\LAVASPORT\DAO\MigrarCadeteDAO.cs:line 125
  </code></pre>
  
  <p>And here is the message</p>
  
  <pre><code>InnerException = {"57014: canceling statement due to statement timeout"}
  </code></pre>
  
  <p>I'm not getting this exceptions all the time, just 1 or 2 times at day. The query in special that most send the exception is a high volume query.</p>
  
  <p>Here is the query:</p>
  
  <pre><code>public IList&lt;Cadete&gt; findByCompania(Compania compania, String conexion,DateTime fechainicial,DateTime fechafinal)
          {
              try
              {
                  DateTime fechaini = new DateTime(fechainicial.Year, fechainicial.Month, fechainicial.Day);
                  DateTime fechafin = new DateTime(fechafinal.Year, fechafinal.Month, fechafinal.Day);
                  var nhConfig = new Configuration().Configure(conexion);
                  var sessionFactory = nhConfig.BuildSessionFactory();
                  var session = sessionFactory.OpenSession();
                  session.BeginTransaction();
                  var query = session.CreateCriteria&lt;Cadete&gt;();
                  query.CreateAlias("compania", "compania");
                  query.Add(Restrictions.Eq("compania.id", compania.id))
                       .Add(Restrictions.Lt("fechaIngreso", fechafin))
                       .Add(Restrictions.Ge("fechaIngreso",fechaini));
                  IList&lt;Cadete&gt; cadetes = query.List&lt;Cadete&gt;();
                  return cadetes;
              }
              catch (Exception ex)
              {
                 MessageBox.Show("Error : " + ex.Message);
              }
              return null;
          }
  </code></pre>
  
  <p>This are my NHibernate configuration and my web config file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" &gt;
    &lt;session-factory name="LAVASPORT"&gt;
      &lt;property name="connection.driver_class"&gt;NHibernate.Driver.NpgsqlDriver&lt;/property&gt;
      &lt;property name="connection.connection_string"&gt;
        Server=localhost;database=LAVASPORT;user id=postgres;password=admin;MaxPoolSize=500;TimeOut=1000;
      &lt;/property&gt;
      &lt;property name="dialect"&gt;NHibernate.Dialect.PostgreSQLDialect&lt;/property&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;
      &lt;property name="query.substitutions"&gt;true 1, false 0, yes 'Y', no 'N'&lt;/property&gt;
      &lt;mapping assembly="LAVASPORT"/&gt;
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>If I increase the timeout to more than 1000, I get errors on connections every time.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/&gt;
      &lt;section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"&gt;&lt;/section&gt;
    &lt;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt;&lt;/configSections&gt;
    &lt;startup&gt; 
        &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/&gt;
    &lt;/startup&gt;
    &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"&gt;
        &lt;parameters&gt;
          &lt;parameter value="v11.0"/&gt;
        &lt;/parameters&gt;
      &lt;/defaultConnectionFactory&gt;
      &lt;providers&gt;
        &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/&gt;
      &lt;/providers&gt;
    &lt;/entityFramework&gt;
    &lt;system.transactions&gt;
      &lt;defaultSettings timeout="10:00:00" /&gt;
    &lt;/system.transactions&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>I would really appreciate any help.</p>