nhibernate与linq查询缓存问题 [英] nhibernate cache problem with linq queries
问题描述
query.Select(x => x).ToList()
但是可以在执行时缓存:
var query = session.Linq< Promoter>();
var p = query.ToList();
两者都产生相同的sql查询,应该是同样的东西。以下测试解释了这个问题。
pre $
public void Can_Use_Cache(
{
ISessionFactory factory = Storage.Application.Get< ISessionFactory>(SessionFactory);
//初始命中数据库并加载到缓存 - 所有罚款
使用(var session = factory.OpenSession())
{
Console.WriteLine();
Console.WriteLine(First Query);
var query = session.Linq< Promoter>();
query.QueryOptions.SetCachable(true);
query.QueryOptions.SetCacheMode(CacheMode.Normal);
var p = query.ToList();
//在数据库中没有命中,并按预期方式从缓存中检索 - 使用(var session = factory.OpenSession())
{
.WriteLine( );
Console.WriteLine(Second Query);
var query = session.Linq< Promoter>();
query.QueryOptions.SetCachable(true);
query.QueryOptions.SetCacheMode(CacheMode.Normal);
var p = query.ToList();
//点击数据库 - 应该来自缓存 - 不工作
使用(var session = factory.OpenSession())
{
控制台。的WriteLine( );
Console.WriteLine(Third Query);
var query = session.Linq< Promoter>();
query.QueryOptions.SetCachable(true);
query.QueryOptions.SetCacheMode(CacheMode.Normal);
var p = query.Select(x => x).ToList();
//再次访问数据库 - 应该来自缓存 - 再次不工作
使用(var session = factory.OpenSession())
{
Console.WriteLine( );
Console.WriteLine(Fourth Query);
var query = session.Linq< Promoter>();
query.QueryOptions.SetCachable(true);
query.QueryOptions.SetCacheMode(CacheMode.Normal);
var p = query.Select(x => x).ToList();
$ / code $ / pre
我的测试结果显示,第二个查询。查询3和4不应该击中数据库:
2010-02-28 12:05:23,046信息开始日志记录
第一个查询
2010-02-28 12:05:23,156 DEBUG选择this_.Id为Id11_0_,this_.Version为Version11_0_,this_.Name为Name11_0_ FROM Promoters this_
NHibernate: SELECT this_.Id as Id11_0_,this_.Version as Version11_0_,this_.Name as Name11_0_ FROM Promoters this_
第二个查询
第三个查询
2010-02-28 12:05:23,315 DEBUG选择this_.Id作为Id11_0_,this_.Version作为Version11_0_,this_.Name作为Name11_0_ FROM Promoters this_
NHibernate:选择this_.Id作为Id11_0_,this_.Version作为Version11_0_,this_.Name作为Name11_0_ FROM Promoters this_
第四个查询
2010-02-28 12:05:23,318 DEBUG选择this_.Id为Id11_0_,this_.Version为Version11_0_,this_.Name为Name11_0_ FROM Promoters this_
NHibernate:选择this_.Id为Id11_0_,this_.Version为Version11_0_,this_.Name为Name11_0_ FROM Promoters this_
缓存配置使用流利:
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(ConfigurationService.SqlConnectionString)
.ShowSql()
.Cache(c => c
.UseQueryCache()
.ProviderClass(typeof(NHibernate.Cache.HashtableCacheProvider).AssemblyQualifiedName))
)
.Mappings(m => m。 FluentMappings.AddFromAssemblyOf< EventMap>()
.Conventions.Setup(MappingConventions.GetConventions()))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
解决方案b
$ bhttp://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions
I am seeing some odd behavior from nhibernate caching and cannot understand the reasoning. I am not able to cache queries when doing select operations like
query.Select(x=>x).ToList()
but can cache when doing:
var query = session.Linq<Promoter>(); var p = query.ToList();
Both produce the same sql query and should be doign the same thing. The following test explains the problem.
[Test] public void Can_Use_Cache() { ISessionFactory factory = Storage.Application.Get<ISessionFactory>("SessionFactory"); // initial hit on the database and load into cache - all fine using (var session = factory.OpenSession()) { Console.WriteLine(""); Console.WriteLine("First Query"); var query = session.Linq<Promoter>(); query.QueryOptions.SetCachable(true); query.QueryOptions.SetCacheMode(CacheMode.Normal); var p = query.ToList(); } // no hit on the database and retrieved from cache as expected - all fine using (var session = factory.OpenSession()) { Console.WriteLine(""); Console.WriteLine("Second Query"); var query = session.Linq<Promoter>(); query.QueryOptions.SetCachable(true); query.QueryOptions.SetCacheMode(CacheMode.Normal); var p = query.ToList(); } // hits the db - should have come from the cache - not working using (var session = factory.OpenSession()) { Console.WriteLine(""); Console.WriteLine("Third Query"); var query = session.Linq<Promoter>(); query.QueryOptions.SetCachable(true); query.QueryOptions.SetCacheMode(CacheMode.Normal); var p = query.Select(x=>x).ToList(); } // hits the db again - should have come from the cache - again not working using (var session = factory.OpenSession()) { Console.WriteLine(""); Console.WriteLine("Fourth Query"); var query = session.Linq<Promoter>(); query.QueryOptions.SetCachable(true); query.QueryOptions.SetCacheMode(CacheMode.Normal); var p = query.Select(x => x).ToList(); } }
My test results showing the hit on the db for the second query. queries 3 and 4 should not be hitting the db:
2010-02-28 12:05:23,046 INFO Started Logging First Query 2010-02-28 12:05:23,156 DEBUG SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_ NHibernate: SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_ Second Query Third Query 2010-02-28 12:05:23,315 DEBUG SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_ NHibernate: SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_ Fourth Query 2010-02-28 12:05:23,318 DEBUG SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_ NHibernate: SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_
The cache is configured using fluent:
SessionFactory = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008 .ConnectionString(ConfigurationService.SqlConnectionString) .ShowSql() .Cache(c => c .UseQueryCache() .ProviderClass(typeof(NHibernate.Cache.HashtableCacheProvider).AssemblyQualifiedName)) ) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<EventMap>() .Conventions.Setup(MappingConventions.GetConventions())) .ExposeConfiguration(BuildSchema) .BuildSessionFactory();
解决方案Have you tried using transactions?
http://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions
这篇关于nhibernate与linq查询缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!