SQL query clears all cache

Description

I hit the problem
http://raffaelschmid.wordpress.com/2013/05/23/native-sql-queries-and-the-hibernate-cache/

Checking with the JavaDoc
http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/SynchronizeableQuery.html#addSynchronizedEntityClass%28java.lang.Class%29
"Adds an entity for ... query result cache invalidation"
If this method is never called, one would expect there is not any cache invalidation.

I suspect there is a bug at
https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java#L164
The if-statement contradicts with comment "@return True if there are affected table spaces and ...".

Attachments

1

Activity

Show:

Samuel Fung November 6, 2015 at 1:45 AM

Account is not a Hibernate managed entity. Does the "Account" space mean anything to Hibernate?

I understand the idea of synchronized space. What confused me is when I did not add any space, the empty set (nothing to be cleared) is equivalent to the universe (everything to be cleared). So the workaround is to add a meaningless space "" to make it non-empty.

Steve Ebersole November 5, 2015 at 12:10 PM

Did I say "use query.addSynchronizedQuerySpace("")"? No. You did.

This idea of a synchronized space is not hard to understand. Just stop and think about it. In your original test, the impact is limited to the "Account" table. So tell Hibernate that:

Samuel Fung November 5, 2015 at 6:19 AM

Do you mean that, in order to keep ALL entity caches,
query.addSynchronizedQuerySpace("");
should be called? This trick is counter-intuitive, and is not mentioned in the doc.

Please also read
https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java#L148
Line 148 contradicts with the @return comment. This is another evidence of bug rather than by design.

Steve Ebersole November 5, 2015 at 3:47 AM

Yes, this works as expected. executeUpdate is different from normal select native queries. If you use executeUpdate with native queries, all entity caches will be invalidated. It makes sense if you step back and think about it.

Steve Ebersole November 5, 2015 at 3:39 AM

Part of you problem is that you are trying to use executeUpdate(). For native queries and executeUpdate() when no "sync spaces" are defined we clear all caches. That is by design.

I'll add some additional tests to make sure that this works as expected in the other cases as well. Assuming they do (and your test does) then I'll be rejecting this.

The work around for your case is to add the synchronized space before issuing the executeUpdate()

Rejected

Details

Assignee

Reporter

Labels

Time tracking

0.35h logged

Components

Affects versions

Priority

Created November 5, 2014 at 9:29 AM
Updated December 3, 2024 at 9:06 AM
Resolved November 5, 2015 at 3:44 AM