13

我们有几个长时间运行的后端进程,它们的运行时间超过了默认的 30 秒。

我们的 NHibernate 版本是 2.0.1.4000,Spring.NET 是 1.2.0.20313。NHibernate 通过 Spring.NET 以这种方式配置:

<object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate20">
    <property name="DbProvider" ref="DbProvider"/> 
    <property name="MappingAssemblies">
        <list>
            <value>SomeKindOfAnItem</value>
        </list> 
    </property>
    <property name="HibernateProperties">
        <dictionary>
            <entry key="expiration" value="120"/>
            <entry key="adonet.batch_size" value="10"/>
            <entry key="cache.provider_class" value="NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache"/>
            <entry key="cache.use_query_cache" value="true"/>
            <entry key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
            <entry key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
            <entry key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
            <entry key="current_session_context_class" value="Spring.Data.NHibernate.SpringSessionContext, Spring.Data.NHibernate20"/>
            <entry key="show_sql" value="false"/>
        </dictionary>
    </property>
</object>

为了解决这个问题,我尝试在 Web.config 中将 NHibernate command_timeout 设置为 60。这是来自 Web.config:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="command_timeout">60</property>
  </session-factory>
</hibernate-configuration>

不幸的是,这不起作用,命令在 30 秒后超时。

我构建了一个控制台应用程序,它像 Web 应用程序一样调用 DAO。我在其配置文件中有完全相同的 NHibernate 配置设置。IDbCommand 在 60 秒而不是 30 秒后超时,使用配置文件中的设置成功。

我尝试调试应用程序并检查从网站调用 DAO 程序集时是否设置了 commandTimeout。它是。

这是来自 Visual Studio 的手表:

((NHibernate.Driver.DriverBase)(((NHibernate.Connection.DriverConnectionProvider) ((NHibernate.Impl.SessionFactoryImpl)session.SessionFactory) .ConnectionProvider).Driver)).commandTimeout: 60

会话是这样创建的:

ISession session = SessionFactoryUtils.GetSession(HibernateTemplate.SessionFactory, true);

我的问题是:如果命令超时字段从我的 Web.config 成功设置为 60,为什么它会在 30 秒后超时?我可以尝试任何想法吗?

4

1 回答 1

12

您可以将超时设置为 hibernate.connection.connection_string 属性的一部分。我没有使用过 Spring.Net,所以我不确定它是如何设置 NHibernate 会话工厂的。如果您能够将“Connect Timeout=120”添加到连接字符串。这应该会将时间从默认的 30 秒超时增加到 120 秒。

以下行将进入web.config

<property name="connection.connection_string">Server=localhost;initial catalog=nhibernate;User Id=;Password=; Connect Timeout=120;</property>

编辑

事实证明,实际上有两个超时。感谢 adomokos 指出这一点。一个用于实际打开连接,另一个用于执行的查询。

用于连接的连接显示在上面,但是要使用 NHibernate 设置查询的超时,请使用该ICriteria界面。

ICriteria crit = session.CreateCriteria(typeof(Foo)); 
crit.SetTimeout(120); 
List<Foo> fooList = crit.List();

超时值以秒为单位。

希望有帮助

于 2010-02-25T07:36:57.607 回答