Hello,
I’m using Hibernate ORM 4.3.1 with JPA, JDK 1.7 and MySQL 5.5.34 in InnoDB Mode.
Now my question. A part of my program loads a record from the db, starts an transaction, modifies and commits. After that, the same record will be loaded and modified again multiple times (but in the same thread)
in some cases (I cannot solid reproduce it), the record has an old state. The code looks like this:
Long documentId = 1234l;
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("TEST");
EntityManager entityManager = null;
try {
entityManager = entityManagerFactory.createEntityManager();
entityManager.setFlushMode(FlushModeType.COMMIT);
//load record first time
entityManager.getTransaction().begin();
Document document = entityManager.find(Document.class, documentId);
document.setStatus("NEW");
entityManager.getTransaction().commit();
} catch(Exception ex) {
entityManager.getTransaction().rollback();
} finally {
entityManager.close();
}
... later....
entityManager = null;
try {
entityManager = entityManagerFactory.createEntityManager();
entityManager.setFlushMode(FlushModeType.COMMIT);
//load record 2nd time
entityManager.getTransaction().begin();
Document document = entityManager.find(Document.class, documentId);
document.setStatus("LOADED");
document.getXYZObject().setText("ABC");
entityManager.getTransaction().commit();
} catch(Exception ex) {
entityManager.getTransaction().rollback();
} finally {
entityManager.close();
}
...later...
entityManager = null;
try {
entityManager = entityManagerFactory.createEntityManager();
entityManager.setFlushMode(FlushModeType.COMMIT);
//load record 3rd time
entityManager.getTransaction().begin();
Document document = entityManager.find(Document.class, documentId);
document.setStatus("PROCESSED");
document.getXYZObject().setText("XYZ");
entityManager.getTransaction().commit();
} catch(Exception ex) {
entityManager.getTransaction().rollback();
} finally {
entityManager.close();
}
In 2nd and 3rd step also a child record will be modified (see getXYZObject()).
Sometimes, at the end all changes to the child are persistent in the database (text ist XYZ) but the Document-Object still has the status NEW.
If I repeat this test multiple times, in some times the data of the Document-Object is as i expected, in some times not.
Some ideas?
Best regards!