Hi,
Hibernate version: 3 RC1
We have a Struts appn that is using the session-per-request-detached object strategy running in Tomcat 4.1.31. The persistence layer is using the HibernateUtil in section 1.4 of the Hibernate docs.
The usecase we're working with goes like this :
(a) list of objects is fetched from datastore (Oracle 9i, using longs for versioning)
(b) a single object is fetched from that list by id and then edited
(c) object is persisted (ok)
(d) list of objects form (a) is retrieved again and displayed
The update in (c) is handled using this method :
Code:
public Object mergeObject(Object obj) throws PersistenceException {
Transaction tx=null;
Object updatedObj=null;
Session session = HibernateUtil.currentSession();
try {
tx = session.beginTransaction();
updatedObj = session.merge(obj);
session.flush();
tx.commit();
} catch (HibernateException e) {
e.printStackTrace(System.out);
if (tx!=null) tx.rollback();
String msg = "Exception MERGING object - rolling back";
log.error(msg,e);
throw new PersistenceException(msg, e);
} finally {
HibernateUtil.closeSession();
}
return updatedObj;
}
We're seeing some weird behaviour where dependent on the Tomcat thread retrieving the list of objects (a), we sometimes see stale data onscreen. This only happens with one out of 3 threads and appears intermittently but reproducibly.
ie after editing an object in the list, and repeatedly refreshing page (a) we get this...
Refresh page (a) - Thread-A : list of objects is up-to-date
Refresh page (a) - Thread-B : list of objects is up-to-date
Refresh page (a) - Thread-C : list of objects is STALE
Refresh page (a) - Thread-A : list of objects is up-to-date
etc
I'd understood that using a ThreadLocal, each thread would get it's own copy of the 1st layer cache. If so, how can we ensure that all replicas are consistent with current data such taht the page refresh returns the latest data on each refresh?
Many thanks!