Invalidate cache entry when deleting with query

143 views
Skip to first unread message

Enrico

unread,
Oct 22, 2009, 6:06:06 AM10/22/09
to hibernate-memcached
Let's say I've got a few instances of an entity A cached.
Now I go and delete these instances from the database through an hql
query like so:

query = manager.createQuery("delete from A a where
a.somefield=:field");
query.setParameter("field", "thanksForTheHelp!");
num = query.executeUpdate();

I was wondering what happens in the cache in this case?
Does hibernate know to clear the cache?
I ask because I seem to be getting some unexpected errors when I
delete entities this way. After restarting the cache and servers these
errors disappear..

Thanks.

Ray Krueger

unread,
Oct 22, 2009, 6:54:56 AM10/22/09
to hibernate...@googlegroups.com
> I was wondering what happens in the cache in this case?
> Does hibernate know to clear the cache?

If you delete records from your database, nothing above the database
will know those records were deleted. This is a really bad thing to be
doing to your application, regardless of your caching layer of choice.

> I ask because I seem to be getting some unexpected errors when I
> delete entities this way. After restarting the cache and servers these
> errors disappear..

You should definitely expect errors if you pull the rug out from under
your application :)

-Ray

Ran Enrico Magen

unread,
Oct 22, 2009, 6:59:35 AM10/22/09
to hibernate...@googlegroups.com
Hmm.. I thought that since my query is not a native mysql query direct to the database, but an HQL query, it has to go through the Hibernate mechanism, and that Hibernate knows, in such cases, to delete entities from all layers..

Ray Krueger

unread,
Oct 22, 2009, 7:00:01 AM10/22/09
to hibernate...@googlegroups.com

Wait! Check that!
I shouldn't reply so quickly to technical emails immediately after
rolling out of bed :P
I didn't read closely and didn't look at the sample there, I thought
you meant you were deleting out of the database with SQL.

I'm really not sure what effect HQL deletes will have off the top of
my head. What sort of errors are you getting?

Ran Enrico Magen

unread,
Oct 22, 2009, 7:06:58 AM10/22/09
to hibernate...@googlegroups.com
EntityNotFoundException

If "the rug problem" is indeed the case, I think that somewhere a deleted entity is still in the cache and it tries to retrieve an instance to another deleted entity it's referencing in a lazy manner.
so the first one is cached, and when lazily looking for the reference it's not found...

Ray Krueger

unread,
Oct 22, 2009, 10:37:36 AM10/22/09
to hibernate...@googlegroups.com
> EntityNotFoundException
>
> If "the rug problem" is indeed the case, I think that somewhere a deleted
> entity is still in the cache and it tries to retrieve an instance to another
> deleted entity it's referencing in a lazy manner.
> so the first one is cached, and when lazily looking for the reference it's
> not found...
>

Again, you should totally ignore the nonsense in my first email.
Unfortunately I don't know the answer to your problem off the top of
my head. Which means you'll have to research this one yourself. It
would be awesome if you posted your findings back for the benefits of
others that may come across the same problem.

My only real theory is that Hibernate cannot guarantee consistency in
the cache when doing HQL deletes like that because it doesn't know
what objects are cached and will be deleted by the query. Perhaps try
not doing deletes like that and instead query up the objects and then
use session.delete on them.

Reply all
Reply to author
Forward
0 new messages