l3dx
l3dx

Reputation: 2955

Hibernate doesn't notice database updates made from other source

I have a small system consisting of a .net client and a java web service.

The .net client inserts an object into the database, and then calls the web service. The web service tries to retrieve this object using hibernate. First time it works fine, but every other time it says that there is no object with the given identifier.

I have checked the database manually and the row is indeed there! (I debugged the web service and checked for the row even before a session was opened).

SOLUTION

Added this to the hibernate config file

<property name="connection.isolation">1</property>

Here's what I've tried so far:

  1. The second level cache is disabled
  2. Added .setCacheMode(CacheMode.REFRESH)

Here's the failing code:

Session session = Program.HibernateUtil.getSessionFactory().openSession();      
try    
{
    return (Alert)session.load(Alert.class, id);                           
} ...

Upvotes: 3

Views: 399

Answers (3)

Nick Holt
Nick Holt

Reputation: 34321

It looks like the second level cache (the one associated with the session factory) should be disabled so the only other thing I can suggest is to explicitly clear the cache with the call:

sessionFactory.evict(Alert.class) 

Note: read the comments for the full answer.

Upvotes: 2

skaffman
skaffman

Reputation: 403581

Firstly, don't use Session.load(), use Session.get(). load() should only be used in very particular situations, and this ain't one of them.

Secondly, are you performing both operations within the same Session? If so, Hibernate will cache the entity after the first operation. There's no way to stop it doing this. However, you can use evict() on the Session to forceably kick the entity out of the session cache.

Upvotes: 1

Peter Thomas
Peter Thomas

Reputation: 58128

Try setting this hibernate property:

hibernate.cache.provider_class = org.hibernate.cache.NoCacheProvider

Upvotes: 0

Related Questions