简体   繁体   English

NHibernate缓存问题

[英]NHibernate Caching Problems

I am having a problem with Nhibernate caching objects and returning old versions: 我在使用Nhibernate缓存对象并返回旧版本时遇到问题:

Here is the code I an running 这是我正在运行的代码

public IList<Foo> GetFoos()
{
  return _session.Query<Foo>().Where(f => f.State == 0).ToList();
}

public void Update(Foo foo)
{
  foo.State = 1;

  _session.Transaction.Begin();
  _session.Update(foo);
  _session.Transaction.Commit();
}

If I have 10 Foos in state 0 in the database and I call GetFoos I get 10 Foo object which have a state of 0, thats great. 如果我在数据库中的状态为0的情况下有10个Foos,并且我调用GetFoos得到状态为0的10个Foo对象, GetFoos太好了。

If I call Update on every Foo the database shows 10 foos in state 1, thats great. 如果我在每个Foo上调用Update ,则数据库会在状态1中显示10个foo。

Now if I call GetFoos I get 0 Foo objects, thats great. 现在,如果我调用GetFoos我会得到0个Foo对象, GetFoos太好了。

If an external application changes the state of all the Foos to state 0, and I run the GetFoos method I get 10 objects returned, that's great. 如果外部应用程序将所有Foos的状态更改为状态0,并且运行GetFoos方法,我将返回10个对象,那就太好了。 However the objects all have a state of 1!!!!!!, thats awful! 但是所有对象的状态均为1 !!!!!!,那太糟糕了!

Now if I call Update on all the foos, the database doesn't update because Nhibernate thinks nothing has changed, so they stay in state 0 in the database forever. 现在,如果我对所有foo都调用Update,则数据库不会更新,因为Nhibernate认为没有任何变化,因此它们永远保持数据库的状态0。

Have I done something wrong with my configuration, or is this expected behaviour? 我的配置有问题吗,还是这种预期的行为? How can I get the objects to return the correct, new, values? 如何获得对象以返回正确的新值?

Thanks 谢谢

The entities loaded by a session (ISession instance) will be kept in first level cache. 会话(ISession实例)加载的实体将保留在一级缓存中。 You can either clear them all (ISession.Clear()) or manually call a ISession.Refresh() on every one to get the current values from the DB. 您可以全部清除它们(ISession.Clear()),也可以手动调用每个ISession.Refresh()以从数据库中获取当前值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM