Ali
Ali

Reputation: 267049

Hibernate update changes record in db, but future requests still return cached values?

I'm updating a record via hibernate with the following code:

        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        User u =  (User) session.get(User.class, userId) ;
        u.fooBar = newFooBar;
        session.update(u);
        session.getTransaction().commit();

When I execute this code, I can see in my database that the value of the column fooBar for the user has changed. However in future requests, when queries are made which would fetch the same User, its fooBar value is the same as it was before updating. If I restart my web server, then the value updates.

Is Hibernate simply caching the old values? How can I force it to update its cache values when the object is updated?

Upvotes: 2

Views: 1295

Answers (1)

user3360944
user3360944

Reputation: 558

If you start each servlet get with a new transaction and close it when you are finished you should get the most recent data.

The code to do this is getCurrentSession().beginTransaction() and t.commit() or t.rollback()

Upvotes: 2

Related Questions