Deepak Singhal
Deepak Singhal

Reputation: 10876

How to remove Entity from Hibernate cache

I am using hibernate, spring, jpa. In a workflow I update an entity; but these updates are not available in another workflow. When I restart the server it works fine.

Is there a way so that when I update an entity; I ask hibernate to remove it from whatever cache it has.. So that when that object is needed by any other workflow a fresh query is made ?

Upvotes: 0

Views: 4741

Answers (1)

jpkroehling
jpkroehling

Reputation: 14061

This sounds like you have two separate sessions for the same app, thus, having two 1st level caches. The first level cache is the one that Hibernate uses for itself, in the context of a session. So, if you don't close/clear your session, this will keep growing, possibly conflicting with other 1st level caches (in other threads or in other VMs). It's hard to say if that's the case, as you didn't specify your environment, but you can't change another session's first level cache.

The best solution to avoid this is to use a managed EntityManager (from your application server) to deal with entities. It's then the server's role to deal with this kind of scenario. But it seems that you are doing it the "spring way", so, you'll have to do it manually: either clear the session after you use it, or do a refresh before reading/updating your data. You'll then need some sort of locking (pessimistic/optimistic) to not lose information that might have been changed from another thread.

Upvotes: 1

Related Questions