I did read about transactions, but what does it have to do with transactions?
In any case, I just dropped transactional code since it is noisy and not related to what I thought (and still think) is a bug.
Another reason I think it is a bug is the following test that passes:
Code:
public void testLockModeNonBug () throws Exception {
Session s = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
// create parent and children
Parent p = new Parent ();
p.setName ("test parent");
s.saveOrUpdate (p);
s.flush ();
s.close ();
// update children collection in one session and try to lock parent in another
Session s0 = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
try {
// retrieve the created parent
Parent p0 = (Parent) s0.load (Parent.class, p.getId ());
p0.setName ("name update");
s0.flush ();
} finally {
try { s0.close (); } catch (Throwable t) {}
}
Session s1 = HibernateSessionFactoryHolder.getSessionFactory ().openSession ();
try {
try {
s1.lock (p, LockMode.READ);
fail ("Object has to be stale still");
} catch (StaleObjectStateException sose) {
// this is correct
}
try {
s1.lock (p, LockMode.READ);
fail ("Object has to be stale still");
} catch (StaleObjectStateException sose) {
// this is correct
}
} finally {
try { s1.close (); } catch (Throwable t) {}
}
}