简体   繁体   English

如何停止Java或Hibernate缓存

[英]How can I stop Java or Hibernate Caching

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. 我认为Java或Hibernate有一些缓存或临时文件来保存数据,因此第二次运行会很快,但是我不希望发生这种情况。 I need monitor the time it actually takes, not the time retrieve from cache or temp file. 我需要监视实际花费的时间,而不是从缓存或临时文件检索的时间。

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). 我试图禁止在Java控制面板中生成缓存和临时文件,我试图禁用休眠缓存(第一级或第二级)。 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 它只是一个简单的应用程序,可从数据库检索数据

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. 如果要强制 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 again with same session object, it will be loaded from cache and no sql query will be executed. 如果我们使用相同的会话对象再次查询相同的对象则会从缓存中加载该对象, 并且不会执行任何SQL查询。
  6. The loaded entity can be removed from session using evict() method. 可以使用evict()方法从会话中删除已加载的实体。 The next loading of this entity will again make a database call if it has been removed using evict() method. 如果已使用evict()方法删除了该实体,则该实体的下一次加载将再次进行数据库调用。
  7. The whole session cache can be removed using clear() method. 可以使用clear()方法删除整个会话缓存。 It will remove all the entities stored in cache. 它将删除所有存储在缓存中的实体。

You should therefor either use the evict() or clear() method to force a query to the database. 因此,您应该使用evict()clear()方法将查询强制到数据库。

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

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-强制不使用缓存来测试查询速度

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

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