I have some transactional code:
import org.hibernate.Session;
void PersistRemovePersist(){
try {
UserTransaction ut = getUt();
EntityManagerFactory emf = getEmf();
A = new some.Entity("A");
ut.begin();
EntityManager em = emf.createEntityManager();
Session ss = em.unwrap(Session.class);
saveOrUpdate(A);
ut.commit();
em.close();
ut.begin(); //TX1
EntityManager em = emf.createEntityManager();
Person a = em.find(Person.class,db.A.getId());
em.remove(a);
ut.commit();
em.close();
ut.begin(); //TX2
em = emf.createEntityManager();
em.unwrap(Session.class).saveOrUpdate(A);
ut.commit(); // <- wrapped StaleObjectStateException
em.close();
}catch (Exception e){
throw new RuntimeException(e);
}
}
At the code execution I get an exception:
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [some.Entity#1]
But why? TX1 and TX2 are serial, not parallel. What am I missing?