NHibernate查询缓存在更新到数据库后不起作用

编程入门 行业动态 更新时间:2024-10-26 07:37:21
本文介绍了NHibernate查询缓存在更新到数据库后不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

流利的.Configure()。数据库(MsSqlConfiguration。 MsSql2005 .ConnectionString(connectionString) .Cache(c => c.ProviderClass< SysCacheProvider>()。UseQueryCache())) .Mappings(m => m.FluentMappings.AddFromAssemblyOf< PersonMap>());

我的映射如下:

当我从存储库调用Persons时,运行:

var query = session.GetSession()。CreateCriteria< Person>(p) .Add(Expression.Eq(p .Org.Id,orgRep.GetOrg()。Id)); query.SetCacheable(true); 返回query.List< Person>()。AsQueryable< Person>();

当我启动应用程序时,一切(包括缓存)都能正常工作。我的第一个查询将会打到数据库,但是下面的查询不会。保存人员时出现问题。 Person保存为:

public virtual void保存(Person p) { if(p。 (> 0& amp; session.GetSession()。 session.GetSession()。合并(p); session.GetSession()。Flush();

保存工作,但是之后缓存没有。查询将始终打到数据库。查看nhibernate日志说:

DEBUG - 检查查询空间的最新性[[Person]] DEBUG - 从缓存中获取对象'NHibernate-Cache:UpdateTimestampsCache:[Person] @ 1639794674'。 DEBUG - 缓存的查询结果不是最新的:sql:SELECT this_.Id作为Id0_0_,this_.Name作为Name0_0_,this_.Org_id作为Org5_0_0_ FROM [Person] this_ WHERE this_.Org_id =?;参数:['1'];第一行:0

我做错了什么?

解决方案

尝试使用块在会话中实例化显式事务。本文看起来很相关: - nhprof/Learn/Alerts/DoNotUseImplicitTransactions

I have enabled 2nd level cache in FluentNHibernate:

Fluently.Configure() .Database(MsSqlConfiguration.MsSql2005 .ConnectionString(connectionString) .Cache(c => c.ProviderClass<SysCacheProvider>().UseQueryCache()) ) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<PersonMap>());

My mapping is as follows:

public PersonMap() { Id(x => x.Id); Map(x => x.Name); Cache.ReadWrite(); }

When I call Persons from my repository, I run:

var query = session.GetSession().CreateCriteria<Person>("p") .Add(Expression.Eq("p.Org.Id", orgRep.GetOrg().Id)); query.SetCacheable(true); return query.List<Person>().AsQueryable<Person>();

When I start the application everything (including cache) works fine. My first query will hit the database, but following ones don't. Problem arises when I save the Person. Person is saved like:

public virtual void Save(Person p) { if (p.Id > 0 && session.GetSession().Get<Person>(p.Id).Org != orgRep.GetOrg()) throw new SecurityException("Organization mismatch"); session.GetSession().Merge(p); session.GetSession().Flush(); }

Saving works but after that the cache doesn't. Queries will always hit the database. Looking through nhibernate log says that:

DEBUG - Checking query spaces for up-to-dateness [[Person]] DEBUG - Fetching object 'NHibernate-Cache:UpdateTimestampsCache:[Person]@1639794674' from the cache. DEBUG - cached query results were not up to date for: sql: SELECT this_.Id as Id0_0_, this_.Name as Name0_0_, this_.Org_id as Org5_0_0_ FROM [Person] this_ WHERE this_.Org_id = ?; parameters: ['1']; first row: 0

What am I doing wrong?

解决方案

Try instantiating an explicit transaction within a session using block. This article looks relevant:- nhprof/Learn/Alerts/DoNotUseImplicitTransactions

更多推荐

NHibernate查询缓存在更新到数据库后不起作用

本文发布于:2023-10-23 19:53:49,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   新到   不起作用   数据库   NHibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!