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

发布于 2024-09-16 20:36:23 字数 1661 浏览 5 评论 0原文

我已在 FluentNHibernate 中启用二级缓存:

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

我的映射如下:

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

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

    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>();

当我启动应用程序时,一切(包括缓存)都工作正常。我的第一个查询将访问数据库,但以下查询不会。当我拯救这个人时,问题就出现了。人的保存方式如下:

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();
}

保存有效,但之后缓存不起作用。查询将始终访问数据库。查看 nhibernate 日志会发现:

 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

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2024-09-23 20:36:23

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文