NHibernate:如何设置连接超时

Igo*_*rup 13 c# nhibernate connection-timeout

在NHibernate连接失败(连接超时)之前,有没有办法全局设置等待连接到给定数据库的时间?在ADO.NET中,您可以为这样的单个连接执行此操作:

new SqlConnection().ConnectionTimeout = 10;
Run Code Online (Sandbox Code Playgroud)

我发现,你会如何设置时间等待一个结果集,命令执行失败之前这里(命令超时).但是,显然,这不是我需要的

amc*_*ott 18

您可以使用command_timeoutNHibernate配置代码中的设置.有关完整详细信息,请参阅文档的第3.4节.

这个的XML配置如下......

<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="command_timeout">100</property>
    </session-factory>
</hibernate-configuration>

<!-- other app specific config follows -->
Run Code Online (Sandbox Code Playgroud)

我正在使用Fluent NHibernate,所以我的配置代码如下......

FluentConfiguration configuration = Fluently.Configure()
                         .Database(MsSqlConfiguration.MsSql2012.ConnectionString(ConnectionString))
                         .ExposeConfiguration(cfg => cfg
                            .SetProperty("command_timeout", "100")
                         .Mappings(m =>
                         {
                             var cfg = CreateAutomappings();
                             m.AutoMappings.Add(cfg);
                         });
Run Code Online (Sandbox Code Playgroud)

  • _command timeout_和_connection timeout_之间存在差异.请参考[此处](https://nirajrules.wordpress.com/2012/02/08/connectiontimeout-vs-commandtimeout/).您提供的代码设置命令超时,但我需要一种方法来设置连接超时 (3认同)

Rog*_*ger 11

您可以在连接字符串"Connection Timeout = x"上设置它.

  • 请注意,此处的"x"是以秒为单位的时间. (3认同)