* the schema disagreement exception
*/
@Test
public void rollbackOnError() throws Exception
{
PersonRedis p = null;
try
{
Object p1 = prepareData("1", 10);
Object p2 = prepareData("2", 20);
em.persist(p1);
em.persist(p2);
p = em.find(PersonRedis.class, "1");
Assert.assertNotNull(p);
Object p3 = prepareData("3", 15);
em.persist(p3);
// Assert on rollback on error.
((PersonRedis) p2).setPersonName("rollback");
em.merge(p2);
em.merge(null);
// As this is a runtime exception so rollback should happen and
// delete out commited data.
}
catch (Exception ex)
{
p = em.find(PersonRedis.class, "1");
Assert.assertNull(p);
p = em.find(PersonRedis.class, "2");
Assert.assertNull(p);
p = em.find(PersonRedis.class, "3");
Assert.assertNull(p);
}
em.clear();
// persist with 1 em
EntityManager em1 = emf.createEntityManager();
// em1.setFlushMode(FlushModeType.COMMIT);
em1.getTransaction().begin();
Object p3 = prepareData("4", 15);
em1.persist(p3);
em1.getTransaction().commit();
try
{
// remove with another em with auto flush.
EntityManager em2 = emf.createEntityManager();
PersonRedis person = em2.find(PersonRedis.class, "4");
em2.remove(person);
em2.merge(null);
}
catch (Exception ex)
{