Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

How to disable hibernate caching

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...