为什么即使故意激发PersistenceException,EntityManager事务rollback()也不起作用?

时间:2015-07-25 09:55:08

标签: jpa eclipselink jpa-2.0 jpa-2.1

PFB我的代码,

        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();
        }
    }

未故意在e2中设置empId属性,以便抛出异常。一旦抛出异常,就应该发生回滚。但我仍然可以看到e1被持久存入数据库。有谁能说出为什么回滚没有发生?

0 个答案:

没有答案