Not rollback in multi-tenancy with hibernate and JakartaEE

44 Views Asked by At

I followed this link to test multi-tenancy with JakartaEE 8, Payara web-server 5.2021.9, Hibernate 5.4.32 and MariaDb 10.5. When I persist and throw exception, my testing entity is not rollback(still persisted in database) even using entityManager.getTransaction().rollback() or setRollbackonly().

Here're my codes:

@Stateless 
public class TestRollBackBean extends Dao {

    @PostConstruct
    public void init() {
        logger.info("is initializing");
    }

    @PreDestroy
    public void detroy() {
        logger.info("is detroying");
    }

    private static final Logger logger = LogManager.getLogger();
 
    public void testCertFormTx(final String tenantId) {
        final EntityManager em = getEntityManager(tenantId);
        final EntityTransaction etx = em.getTransaction();
        final CertificateForm cf = new CertificateForm();//ntity
        try {
            logger.debug("test persist and rollback for CertificateForm");
            
            cf.setActive(true);
            cf.setDesp("testing"); 
            em.persist(cf); 
            logger.debug("new Id: {}", () -> cf.getId());
            throw new Exception("test rollback now");
        } catch (Exception e) { 
            etx.setRollbackOnly(); 
        } finally {
            if (em != null && em.isOpen()) { 
                em.close();
            }
        }
    }
}

What I'm wrong or missing?

0

There are 0 best solutions below