如何停止Java或Hibernate缓存 [英] How can I stop Java or Hibernate Caching

查看:300
本文介绍了如何停止Java或Hibernate缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以从数据库中检索数据,并且我会监控我的应用程序检索数据所需的时间。

I have an app to retrieve data from Database, and I monitor the time my app takes to retrieve data.

但是当我使用相同内容时我遇到了问题数据输入设置为使用我的应用程序检索数据,第二次检索将花费更少的时间。

But I have an issue when I use the same data input set to retrieve data with my app, the second time retrieving will take much less time.

我假设Java或Hibernate有一些缓存或临时文件来保存数据,所以第二次运行会很快,但我不希望它发生。我需要监视它实际需要的时间,而不是从缓存或临时文件中检索的时间。

I assume Java or Hibernate has some cache or temp file to save the data, so second time run will be fast, but I don't want it happen. I need monitor the time it actually takes, not the time retrieve from cache or temp file.

我试图禁止在Java控制面板中生成缓存和临时文件,I试图禁用hibernate缓存(第一级或第二级)。但这些仍然无法解决我的问题。第二次运行仍然花费的时间比应该花费的时间少。

I tried to forbid the cache and temp file generate in Java control Panel, I tried to disable the hibernate cache(first level or second level). But these are still not solve my problem. The second time run still takes less time than it should take.

任何想法导致第二次运行更快的原因?它只是一个从数据库中检索数据的简单应用程序

Any idea the reason caused the second time run faster? it just a simple app to retrieve data from DB

推荐答案

Hibernate一级缓存无法禁用(请参阅如何禁用hibernate缓存)。如果你想强制 Hibernate查询到数据库,你需要了解Hibernate的会话缓存。

The Hibernate 1st level cache can not be disabled (see How to disable hibernate caching). You need to understand Hibernate's session cache if you want to force Hibernate querying to the database.

Lokesh Gupta有一个关于 http://howtodoinjava.com/2013/07/01 / understanding-hibernate-first-level-cache-with-example /

Lokesh Gupta has a good tutorial on http://howtodoinjava.com/2013/07/01/understanding-hibernate-first-level-cache-with-example/



  1. 第一级cache与session对象相关联,应用程序中的其他
    会话对象无法看到它。

  2. 缓存对象的范围是会话。会话关闭后,
    缓存对象将永远消失。

  3. 默认启用一级缓存,无法禁用

  4. 当我们第一次查询实体时,它会从数据库
    中检索并存储在与hibernate会话关联的第一级缓存中。

  5. 如果我们使用相同的会话对象再次查询同一个对象将从缓存加载
    ,并且不执行任何sql查询。

  6. 可以使用evict()方法从会话中删除加载的实体。
    如果使用evict删除
    ,则此实体的下一次加载将再次进行数据库调用( )方法。

  7. 可以使用clear()方法删除整个会话缓存。它将
    删除存储在缓存中的所有实体。

  1. First level cache is associated with "session" object and other session objects in application can not see it.
  2. The scope of cache objects is of session. Once session is closed, cached objects are gone forever.
  3. First level cache is enabled by default and you can not disable it.
  4. When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session.
  5. If we query same object again with same session object, it will be loaded from cache and no sql query will be executed.
  6. The loaded entity can be removed from session using evict() method. The next loading of this entity will again make a database call if it has been removed using evict() method.
  7. The whole session cache can be removed using clear() method. It will remove all the entities stored in cache.


你应该为此使用 evict() clear()强制查询数据库的方法。

You should therefor either use the evict() or clear() method to force a query to the database.

为了验证这一点,你可以使用 hibernate.show_sql 配置属性打开SQL输出(参见 https://docs.jboss.org/hibernate/orm/5.0 /manual/en-US/html/ch03.html#configuration-optional )。

In order to verify this, you can turn on SQL output using the hibernate.show_sql configuration property (see https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch03.html#configuration-optional).

这篇关于如何停止Java或Hibernate缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆