如何禁用休眠缓存 [英] How to disable hibernate caching

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

问题描述

我正在尝试编写一个单元测试类,它必须使用相同的查询以相同的测试方法从数据库中获取结果两次。但是由于第二次启用了Hibernate缓存,它实际上并不会触及数据库,只是从缓存中获取结果。



有人可以请回答如何禁用<$ c中的缓存$ c $> persistence.xml 。



我尝试通过更改属性 hibernate.cache.use.query_cache = false hibernate.cache.use_second_level_cache = false




解决方案


有人可以请回答如何在persistence.xml中禁用缓存。


默认情况下禁用二级缓存和查询缓存(除非明确缓存查询,否则查询不会被缓存) 。一级缓存不能被禁用。


我尝试通过更改属性(...)来禁用

这将禁用二级缓存和查询缓存,如果它们已启用。


但它不起作用。

说实话,它没有用是对当前行为与预期行为的很差描述。提供更多详细信息(伪)代码,SQL跟踪可能会有所帮助。

这就是说,如果问题是关于HQL的,HQL查询肯定会碰到数据库后续执行(没有任何查询缓存)。如果需要观察这个,激活SQL日志记录。



如果问题是关于 Session#get() Session#load(),那么你可以使用 Session#refresh()重新加载实体的状态,或者调用 Session#clear()完全清除会话。


I am trying to write a unit test class which will have to use same query to fetch the results from database two times in same test method. But as Hibernate cache is enabled second time it is not actually hitting the database and simply fetching the results from cache.

Can someone please answer how to disable caching in persistence.xml.

I tried to disable by changing properties hibernate.cache.use.query_cache = false and hibernate.cache.use_second_level_cache = false.

But It did not work.

解决方案

Can someone please answer how to disable caching in persistence.xml.

The second-level cache and query cache are disabled by default (and queries are not cached unless you explicitly cache them). The first-level cache can't be disabled.

I tried to disable by changing properties (...)

This would disable the second-level cache and query cache, if they were enabled.

But It did not work.

To be honest, "it did not work" is a very poor description of the current behavior vs the expected one. Providing more details, (pseudo) code, SQL traces would probably help.

That being said, if the question is about HQL, an HQL query should definitely hit the database upon subsequent execution (without any query cache). Activate SQL logging if required to observe this.

If the question is about Session#get() or Session#load(), then you could reload the state of an entity using Session#refresh() or call Session#clear() to completely clear the session.

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

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