Search in sources :

Example 6 with RollbackException

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

the class FlushAndTransactionTest method testRollbackExceptionOnOptimisticLockException.

@Test
public void testRollbackExceptionOnOptimisticLockException() throws Exception {
    Book book = new Book();
    book.name = "Stolen keys";
    //new Integer( 50 );
    book.id = null;
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(book);
    em.flush();
    em.clear();
    book.setName("kitty kid");
    em.merge(book);
    em.flush();
    em.clear();
    //non updated version
    book.setName("kitty kid2");
    em.unwrap(Session.class).update(book);
    try {
        em.getTransaction().commit();
        fail("Commit should be rollbacked");
    } catch (RollbackException e) {
        assertTrue("During flush a StateStateException is wrapped into a OptimisticLockException", e.getCause() instanceof OptimisticLockException);
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) OptimisticLockException(javax.persistence.OptimisticLockException) RollbackException(javax.persistence.RollbackException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with RollbackException

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

the class TransactionCommitFailureTest method testConfiguredInterceptor.

@Test
public void testConfiguredInterceptor() {
    Map settings = basicSettings();
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
    EntityManager em = emf.createEntityManager();
    try {
        em.getTransaction().begin();
        transactionFailureTrigger.set(true);
        em.getTransaction().commit();
    } catch (RollbackException e) {
        assertEquals(COMMIT_FAILURE, e.getCause().getMessage());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
        emf.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) RollbackException(javax.persistence.RollbackException) Map(java.util.Map) Test(org.junit.Test)

Example 8 with RollbackException

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

the class BeanValidationTest method testBeanValidationIntegrationOnCommit.

@Test
public void testBeanValidationIntegrationOnCommit() {
    CupHolder ch = new CupHolder();
    ch.setRadius(new BigDecimal("9"));
    ch.setTitle("foo");
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(ch);
    em.flush();
    try {
        ch.setRadius(new BigDecimal("12"));
        em.getTransaction().commit();
        fail("invalid object should not be persisted");
    } catch (RollbackException e) {
        final Throwable cve = e.getCause();
        assertTrue(cve instanceof ConstraintViolationException);
        assertEquals(1, ((ConstraintViolationException) cve).getConstraintViolations().size());
    }
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) ConstraintViolationException(javax.validation.ConstraintViolationException) RollbackException(javax.persistence.RollbackException) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 9 with RollbackException

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

the class FlushAndTransactionTest method testRollbackOnlyOnPersistenceException.

@Test
public void testRollbackOnlyOnPersistenceException() throws Exception {
    Book book = new Book();
    book.name = "Stolen keys";
    //new Integer( 50 );
    book.id = null;
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        em.persist(book);
        em.flush();
        em.clear();
        book.setName("kitty kid");
        em.merge(book);
        em.flush();
        em.clear();
        //non updated version
        book.setName("kitty kid2");
        em.merge(book);
        em.flush();
        fail("optimistic locking exception");
    } catch (PersistenceException e) {
    //success
    }
    try {
        em.getTransaction().commit();
        fail("Commit should be rollbacked");
    } catch (RollbackException e) {
    //success
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) 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