NHibernate的:条件查询的Web应用程序缓慢,但在快速单元测试,为什么呢? [英] NHibernate: Criteria query slow in web app but fast in unit tests, why?
问题描述
我有其中NHibernate的条件查询是当我在单元测试运行它执行在不到第二个问题,但是当我尝试从我的web应用的上下文中运行,它需要花费一分钟。两者都击中相同的数据库相同的数据。
I am having a problem where a criteria query in NHibernate is executes in less then a second when I run it in a unit test, but when I try to run it from the context of my web application, it takes over a minute. Both are hitting the same database for the same data.
我的NHibernate的映射:
My NHibernate mapping:
var properties = new Dictionary<string, string>();
var configuration = new Configuration();
properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("proxyfactory.factory_class", "NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate");
properties.Add("connection.release_mode", "on_close");
properties.Add("current_session_context_class", "web");
properties.Add("dialect", "NHibernate.Dialect.MsSql2005Dialect");
properties.Add("connection.connection_string_name", "DBConnection");
configuration.Properties = properties;
SessionFactory = configuration.BuildSessionFactory();
在测试和网络应用程序之间的映射的唯一区别是current_session_context_class,它是在测试中thread_static,但似乎并不成为问题。
The only difference in this mapping between tests and the web app is the current_session_context_class, where it is thread_static in tests, but that does not seem to be the problem.
用于查询的标准:
var reports = Session.CreateCriteria<Report>()
.SetFetchMode("Site", FetchMode.Join)
.SetFetchMode("Actions", FetchMode.Join)
.SetResultTransformer(new DistinctRootEntityResultTransformer())
.Add(Subqueries.PropertyIn("Site",
SiteCriteria.GetSitesForUserWithPermission(user, Permission.SomePermission))))
.List<Report>();
我已经用NH Profiler来帮助试过了,但它并没有提供任何有用的建议。
I have tried using NH Profiler to help, but it did not offer any useful suggestions.
编辑:在nhprofiler进一步看,我看到,在试验中,例如,查询时间为1ms / 313ms(数据库只/总)。
但对于网站,它只是花了我1毫秒/ 43698ms。似乎是NHibernate的有一个很难映射的实际对象。
edit: Looking further in nhprofiler, I see that in the test for example, the query duration is 1ms / 313ms (Database only / Total). But for the website it just took me 1ms / 43698ms. It seems that NHibernate is having a hard time mapping the actual objects.
推荐答案
单元测试和Web应用程序之间的区别是,单元测试不被记录。
我加入到我们的log4net.config:
The difference between the unit tests and the web app is that unit tests aren't logged. I added to our log4net.config:
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
</filter>
和问题走了。
据输出了很多的东西是这样的:
It was outputting a lot of stuff like this:
2011-07-21 13:07:17,479 DEBUG [14] LoadContexts - attempting to locate loading collection entry [CollectionKey[Actions#d6adfe87-a7d4-4821-bb10-4ef76fcf614d]] in any result-set context
2011-07-21 13:07:17,481 DEBUG [14] LoadContexts - collection [CollectionKey[Actions#d6adfe87-a7d4-4821-bb10-4ef76fcf614d]] not located in load context
这篇关于NHibernate的:条件查询的Web应用程序缓慢,但在快速单元测试,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!