今天,我遇到了超时问题。
我具有用于创建SessionFactory的以下配置:
<property name="adonet.batch_size">50</property>
<property name="command_timeout">600</property>
我没有将其存储在web.config中,而是存储在一个手动传递给配置的XML文件中:
configuration.Configure(cfgFile)
因此,我可以拥有多个具有独立配置的 session 工厂(每个数据库)。
但是
command_timeout
似乎仅在NHibernate不使用批处理时才有效。如果SQL命令是批处理的,那么对于一些大批处理,我得到:NHibernate.Exceptions.GenericADOException: could not execute batch command.
[SQL: SQL not available] --->
System.Data.SqlClient.SqlException: Timeout expired.
The timeout period elapsed prior to completion of the operation or the server is not responding.
在搜寻解决方案时,我发现了一篇文章,解释了为什么会这样:
http://ronaldrosiernet.azurewebsites.net/Blog/2013/04/20/timeout_in_nhibernate_batched_sessions
问题的原因在于,对于SQL批处理,NHibernate使用Cfg.Environment.CommandTimeout而不是在创建 session 时传递给配置的
command_timeout
。我在创建配置时找到了一种解决方法:
if (configuration.Properties.ContainsKey(NHibernate.Cfg.Environment.CommandTimeout))
NHibernate.Cfg.Environment.Properties[NHibernate.Cfg.Environment.CommandTimeout] =
configuration.Properties[NHibernate.Cfg.Environment.CommandTimeout];
现在我的同事们说超时似乎已经解决了。
但是令我困惑的是以下线程:
https://forum.hibernate.org/viewtopic.php?f=25&t=983105
上面写着:
The property NHibernate.Cfg.Environment.Properties returns you a copy of the global properties, so you cannot modify it.
如果NHibernate.Cfg.Environment.Properties是只读副本,那么为什么我的解决方法似乎工作正常?它是否稳定,或者此修复程序不可靠,并且在某些其他情况下可能会中断?
我也在NHibernate JIRA中发现了一个相关的问题:
https://nhibernate.jira.com/browse/NH-2153
如果他们说他们解决了v3.1.0。中的command_timeout问题,那么为什么我仍然必须在NHibernate v3.3.2中使用我的解决方法。 ?
有人对此有见识吗?
最佳答案
使用批处理时,我遇到了同样的问题。 Nhibernate类SqlClientBatchingBatcher使用只读的Environment.GlobalProperties中的命令超时。我发现只有两种方法可以在SqlClientBatchingBatcher.currentBatch命令上设置超时
1)在app.config文件中使用超时
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="command_timeout">120</property>
</session-factory>
</hibernate-configuration>
2)设置环境。
FieldInfo field = typeof(global::NHibernate.Cfg.Environment).GetField("GlobalProperties", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
Dictionary<string, string> gloablProperties = field.GetValue(null) as Dictionary<string, string>;
gloablProperties.Add("command_timeout","120");
关于NHibernate command_timeout不适用于批处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16445583/