Search in sources :

Example 1 with RollbackException

use of javax.persistence.RollbackException in project spring-framework by spring-projects.

the class JpaTransactionManager method doCommit.

@Override
protected void doCommit(DefaultTransactionStatus status) {
    JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
    if (status.isDebug()) {
        logger.debug("Committing JPA transaction on EntityManager [" + txObject.getEntityManagerHolder().getEntityManager() + "]");
    }
    try {
        EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
        tx.commit();
    } catch (RollbackException ex) {
        if (ex.getCause() instanceof RuntimeException) {
            DataAccessException dex = getJpaDialect().translateExceptionIfPossible((RuntimeException) ex.getCause());
            if (dex != null) {
                throw dex;
            }
        }
        throw new TransactionSystemException("Could not commit JPA transaction", ex);
    } catch (RuntimeException ex) {
        // Assumably failed to flush changes to database.
        throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect());
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.persistence.RollbackException) DataAccessException(org.springframework.dao.DataAccessException)

Example 2 with RollbackException

use of javax.persistence.RollbackException in project spring-framework by spring-projects.

the class JpaTransactionManagerTests method testTransactionCommitWithRollbackException.

@Test
public void testTransactionCommitWithRollbackException() {
    given(manager.getTransaction()).willReturn(tx);
    given(tx.getRollbackOnly()).willReturn(true);
    willThrow(new RollbackException()).given(tx).commit();
    final List<String> l = new ArrayList<>();
    l.add("test");
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    try {
        Object result = tt.execute(new TransactionCallback() {

            @Override
            public Object doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.hasResource(factory));
                EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
                return l;
            }
        });
        assertSame(l, result);
    } catch (TransactionSystemException tse) {
        // expected
        assertTrue(tse.getCause() instanceof RollbackException);
    }
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(manager).flush();
    verify(manager).close();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.persistence.RollbackException) Test(org.junit.Test)

Example 3 with RollbackException

use of javax.persistence.RollbackException in project spring-framework by spring-projects.

the class JpaTransactionManagerTests method testParticipatingTransactionWithRollbackOnly.

@Test
public void testParticipatingTransactionWithRollbackOnly() {
    given(manager.getTransaction()).willReturn(tx);
    given(tx.isActive()).willReturn(true);
    given(tx.getRollbackOnly()).willReturn(true);
    willThrow(new RollbackException()).given(tx).commit();
    final List<String> l = new ArrayList<>();
    l.add("test");
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    try {
        tt.execute(new TransactionCallback() {

            @Override
            public Object doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.hasResource(factory));
                return tt.execute(new TransactionCallback() {

                    @Override
                    public Object doInTransaction(TransactionStatus status) {
                        EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
                        status.setRollbackOnly();
                        return null;
                    }
                });
            }
        });
        fail("Should have thrown TransactionSystemException");
    } catch (TransactionSystemException tse) {
        // expected
        assertTrue(tse.getCause() instanceof RollbackException);
    }
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(manager).flush();
    verify(tx).setRollbackOnly();
    verify(manager).close();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.persistence.RollbackException) Test(org.junit.Test)

Example 4 with RollbackException

use of javax.persistence.RollbackException in project hibernate-orm by hibernate.

the class MultiCircleJpaCascadeTest method testPersistThenUpdateNoCascadeToTransient.

@Test
public void testPersistThenUpdateNoCascadeToTransient() {
    // expected to fail, so nothing will be persisted.
    skipCleanup = true;
    // remove elements from collections and persist
    c.getBCollection().clear();
    c.getDCollection().clear();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(c);
    // now add the elements back
    c.getBCollection().add(b);
    c.getDCollection().add(d);
    try {
        em.getTransaction().commit();
        fail("should have thrown IllegalStateException");
    } catch (RollbackException ex) {
        assertTrue(ex.getCause() instanceof IllegalStateException);
        IllegalStateException ise = (IllegalStateException) ex.getCause();
        // should fail on entity g (due to no cascade to f.g);
        // instead it fails on entity e ( due to no cascade to d.e)
        // because e is not in the process of being saved yet.
        // when HHH-6999 is fixed, this test should be changed to
        // check for g and f.g
        //noinspection ThrowableResultOfMethodCallIgnored
        TransientPropertyValueException tpve = assertTyping(TransientPropertyValueException.class, ise.getCause());
        assertEquals(E.class.getName(), tpve.getTransientEntityName());
        assertEquals(D.class.getName(), tpve.getPropertyOwnerEntityName());
        assertEquals("e", tpve.getPropertyName());
    }
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) TransientPropertyValueException(org.hibernate.TransientPropertyValueException) RollbackException(javax.persistence.RollbackException) Test(org.junit.Test)

Example 5 with RollbackException

use of javax.persistence.RollbackException in project hibernate-orm by hibernate.

the class PersistTest method testCreateException.

@Test
public void testCreateException() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Node dupe = new Node("dupe");
    em.persist(dupe);
    em.persist(dupe);
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(dupe);
    try {
        em.getTransaction().commit();
        fail("Cannot persist() twice the same entity");
    } catch (Exception cve) {
    //verify that an exception is thrown!
    }
    em.close();
    Node nondupe = new Node("nondupe");
    nondupe.addChild(dupe);
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(nondupe);
    try {
        em.getTransaction().commit();
        assertFalse(true);
    } catch (RollbackException e) {
    //verify that an exception is thrown!
    }
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) RollbackException(javax.persistence.RollbackException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) Test(org.junit.Test)

Aggregations

RollbackException (javax.persistence.RollbackException)9 Test (org.junit.Test)8 EntityManager (javax.persistence.EntityManager)6 TransactionSystemException (org.springframework.transaction.TransactionSystemException)3 ArrayList (java.util.ArrayList)2 PersistenceException (javax.persistence.PersistenceException)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 TransactionCallback (org.springframework.transaction.support.TransactionCallback)2 BigDecimal (java.math.BigDecimal)1 Map (java.util.Map)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 EntityTransaction (javax.persistence.EntityTransaction)1 OptimisticLockException (javax.persistence.OptimisticLockException)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 Session (org.hibernate.Session)1 TransientPropertyValueException (org.hibernate.TransientPropertyValueException)1 PersistenceUnitDescriptorAdapter (org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter)1 DataAccessException (org.springframework.dao.DataAccessException)1