PFB my code,
try {
entityManager = emFactory.createEntityManager();
emTransaction = entityManager.getTransaction();
emTransaction.begin();
EmployeeEntity e1 = new EmployeeEntity();
e1.setEmpId(1);
e1.setEmpName("Dheepan");
EmployeeEntity e2 = new EmployeeEntity();
e2.setEmpName("Raju");
entityManager.persist(e1);
entityManager.persist(e2);
emTransaction.commit();
}catch(PersistenceException e){
if(emTransaction!=null && emTransaction.isActive()){
emTransaction.rollback();
}
}
empId attribute is not set in e2 intentionally so that exception will be thrown. As soon exception thrown, rollback should happen. But still I am able to see e1 got persisted into the database. Could anybody tell why rollback didn't happen?