我在使用Npgsql的一些查询中遇到随机错误。
这是堆栈跟踪:
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
这是消息
InnerException = {"57014: canceling statement due to statement timeout"}
我没有一直得到这个例外,一天只有1或2次。大多数发送异常的特殊查询是高容量查询。
以下是查询:
public IList<Cadete> 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<Cadete>();
query.CreateAlias("compania", "compania");
query.Add(Restrictions.Eq("compania.id", compania.id))
.Add(Restrictions.Lt("fechaIngreso", fechafin))
.Add(Restrictions.Ge("fechaIngreso",fechaini));
IList<Cadete> cadetes = query.List<Cadete>();
return cadetes;
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
return null;
}
这是我的NHibernate配置和我的网络配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="LAVASPORT">
<property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="connection.connection_string">
Server=localhost;database=LAVASPORT;user id=postgres;password=admin;MaxPoolSize=500;TimeOut=1000;
</property>
<property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
<property name="show_sql">true</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<mapping assembly="LAVASPORT"/>
</session-factory>
</hibernate-configuration>
如果我将超时增加到1000以上,每次都会出现连接错误。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"></section>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
<system.transactions>
<defaultSettings timeout="10:00:00" />
</system.transactions>
</configuration>
我真的很感激任何帮助。
答案 0 :(得分:0)
首先,请注意Timeout
连接字符串参数管理连接超时(即NpgsqlConnection.Open()
)而不是命令执行超时。默认命令执行超时通过Command Timeout
连接字符串参数进行管理。
除此之外,似乎你的命令只是超时。由于默认命令超时为30秒,因此您的查询,数据库索引或其他内容似乎存在严重的性能问题。您需要仔细分析NHibernate创建的SQL以及PostgreSQL如何执行它。