如何停止 Java 或 Hibernate 缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34162560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:33:06  来源:igfitidea点击:

How can I stop Java or Hibernate Caching

javahibernatecaching

提问by Neil

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.

但是当我使用相同的数据输入集通过我的应用程序检索数据时,我遇到了一个问题,第二次检索将花费更少的时间。

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 或 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.

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

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

知道导致第二次运行速度更快的原因吗?它只是一个从数据库检索数据的简单应用程序

回答by boskoop

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 forceHibernate querying to the database.

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

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

Lokesh Gupta 在http://howtodoinjava.com/2013/07/01/understanding-hibernate-first-level-cache-with-example/上有一个很好的教程

  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 againwith 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.
  1. 一级缓存与“会话”对象相关联,应用程序中的其他会话对象无法看到它。
  2. 缓存对象的范围是会话。一旦会话关闭,缓存的对象就永远消失了。
  3. 默认情况下启用一级缓存,您无法禁用它
  4. 当我们第一次查询实体时,它会从数据库中检索并存储在与休眠会话关联的一级缓存中。
  5. 如果我们使用同一个会话对象再次查询同一个对象,它将从缓存中加载并且不会执行任何 sql 查询。
  6. 可以使用 evict() 方法从会话中删除加载的实体。如果已使用 evict() 方法将其删除,则该实体的下一次加载将再次进行数据库调用。
  7. 可以使用 clear() 方法删除整个会话缓存。它将删除存储在缓存中的所有实体。

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

因此,您应该使用evict()orclear()方法来强制对数据库进行查询。

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

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

回答by Jazzepi

Have you tried disabling the cache in the database itself?

您是否尝试过禁用数据库本身的缓存?

I believe that Hibernate first and second level caches are Hibernate specific, but the database will still cache under the hood.

我相信 Hibernate 的第一级和第二级缓存是 Hibernate 特定的,但数据库仍然会在后台缓存。

MySQL - force not to use cache for testing speed of query

MySQL - 强制不使用缓存来测试查询速度