Search in sources :

Example 26 with SystemException

use of javax.transaction.SystemException in project hibernate-orm by hibernate.

the class JtaIsolationDelegate method doInNewTransaction.

private <T> T doInNewTransaction(HibernateCallable<T> callable, TransactionManager transactionManager) {
    try {
        // start the new isolated transaction
        transactionManager.begin();
        try {
            T result = callable.call();
            // if everything went ok, commit the isolated transaction
            transactionManager.commit();
            return result;
        } catch (Exception e) {
            try {
                transactionManager.rollback();
            } catch (Exception ignore) {
                LOG.unableToRollbackIsolatedTransaction(e, ignore);
            }
            throw new HibernateException("Could not apply work", e);
        }
    } catch (SystemException e) {
        throw new HibernateException("Unable to start isolated transaction", e);
    } catch (NotSupportedException e) {
        throw new HibernateException("Unable to start isolated transaction", e);
    }
}
Also used : SystemException(javax.transaction.SystemException) HibernateException(org.hibernate.HibernateException) NotSupportedException(javax.transaction.NotSupportedException) NotSupportedException(javax.transaction.NotSupportedException) SQLException(java.sql.SQLException) SystemException(javax.transaction.SystemException) HibernateException(org.hibernate.HibernateException)

Example 27 with SystemException

use of javax.transaction.SystemException in project hibernate-orm by hibernate.

the class JtaIsolationDelegate method doInSuspendedTransaction.

private <T> T doInSuspendedTransaction(HibernateCallable<T> callable) {
    try {
        // First we suspend any current JTA transaction
        Transaction surroundingTransaction = transactionManager.suspend();
        LOG.debugf("Surrounding JTA transaction suspended [%s]", surroundingTransaction);
        boolean hadProblems = false;
        try {
            return callable.call();
        } catch (HibernateException e) {
            hadProblems = true;
            throw e;
        } finally {
            try {
                transactionManager.resume(surroundingTransaction);
                LOG.debugf("Surrounding JTA transaction resumed [%s]", surroundingTransaction);
            } catch (Throwable t) {
                // if the actually work had an error use that, otherwise error based on t
                if (!hadProblems) {
                    //noinspection ThrowFromFinallyBlock
                    throw new HibernateException("Unable to resume previously suspended transaction", t);
                }
            }
        }
    } catch (SystemException e) {
        throw new HibernateException("Unable to suspend current JTA transaction", e);
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) HibernateException(org.hibernate.HibernateException)

Example 28 with SystemException

use of javax.transaction.SystemException in project hibernate-orm by hibernate.

the class TransactionRolledBackInDifferentThreadTest method testTransactionRolledBackInDifferentThreadFailure.

@Test
public void testTransactionRolledBackInDifferentThreadFailure() throws Exception {
    /**
		 * The three test threads share the same entity manager.
		 * The main test thread creates an EntityManager, joins it to the transaction and ends the transaction.
		 * Test thread 1 joins the EntityManager to its transaction, sets rollbackonly and ends the transaction.
		 * Test thread 2 attempts to join the EntityManager to its transaction but will fail with a
		 *   HibernateException("Transaction was rolled back in a different thread!")
		 */
    // main test thread
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    final EntityManager em = entityManagerFactory().createEntityManager();
    em.joinTransaction();
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
    // will be set to the failing exception
    final HibernateException[] transactionRolledBackInDifferentThreadException = new HibernateException[2];
    transactionRolledBackInDifferentThreadException[0] = transactionRolledBackInDifferentThreadException[1] = null;
    // background test thread 1
    final Runnable run1 = new Runnable() {

        @Override
        public void run() {
            try {
                TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
                em.joinTransaction();
                TestingJtaPlatformImpl.INSTANCE.getTransactionManager().setRollbackOnly();
                TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
            } catch (javax.persistence.PersistenceException e) {
                if (e.getCause() instanceof HibernateException && e.getCause().getMessage().equals("Transaction was rolled back in a different thread!")) {
                    /**
						 * Save the exception for the main test thread to fail
						 */
                    // show the error first
                    e.printStackTrace();
                    transactionRolledBackInDifferentThreadException[0] = (HibernateException) e.getCause();
                }
            } catch (RollbackException ignored) {
            // expected to see RollbackException: ARJUNA016053: Could not commit transaction.
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            } finally {
                try {
                    if (TestingJtaPlatformImpl.INSTANCE.getTransactionManager().getStatus() != Status.STATUS_NO_TRANSACTION) {
                        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
                    }
                } catch (SystemException ignore) {
                }
            }
        }
    };
    // test thread 2
    final Runnable run2 = new Runnable() {

        @Override
        public void run() {
            try {
                TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
                /**
					 * the following call to em.joinTransaction() will throw:
					 *   org.hibernate.HibernateException: Transaction was rolled back in a different thread!
					 */
                em.joinTransaction();
                TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
            } catch (javax.persistence.PersistenceException e) {
                if (e.getCause() instanceof HibernateException && e.getCause().getMessage().equals("Transaction was rolled back in a different thread!")) {
                    /**
						 * Save the exception for the main test thread to fail
						 */
                    // show the error first
                    e.printStackTrace();
                    transactionRolledBackInDifferentThreadException[1] = (HibernateException) e.getCause();
                }
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            } finally {
                try {
                    if (TestingJtaPlatformImpl.INSTANCE.getTransactionManager().getStatus() != Status.STATUS_NO_TRANSACTION) {
                        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
                    }
                } catch (SystemException ignore) {
                }
            }
        }
    };
    Thread thread = new Thread(run1, "test thread1");
    thread.start();
    thread.join();
    Thread thread2 = new Thread(run2, "test thread2");
    thread2.start();
    thread2.join();
    // show failure for exception caught in run2.run()
    if (transactionRolledBackInDifferentThreadException[0] != null || transactionRolledBackInDifferentThreadException[1] != null) {
        fail("failure in test thread 1 = " + (transactionRolledBackInDifferentThreadException[0] != null ? transactionRolledBackInDifferentThreadException[0].getMessage() : "(none)") + ", failure in test thread 2 = " + (transactionRolledBackInDifferentThreadException[1] != null ? transactionRolledBackInDifferentThreadException[1].getMessage() : "(none)"));
    }
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) SystemException(javax.transaction.SystemException) HibernateException(org.hibernate.HibernateException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test)

Example 29 with SystemException

use of javax.transaction.SystemException in project Activiti by Activiti.

the class JtaTransactionContext method rollback.

public void rollback() {
    // managed transaction, mark rollback-only if not done so already.
    try {
        Transaction transaction = getTransaction();
        int status = transaction.getStatus();
        if (status != Status.STATUS_NO_TRANSACTION && status != Status.STATUS_ROLLEDBACK) {
            transaction.setRollbackOnly();
        }
    } catch (IllegalStateException e) {
        throw new ActivitiException("Unexpected IllegalStateException while marking transaction rollback only");
    } catch (SystemException e) {
        throw new ActivitiException("SystemException while marking transaction rollback only");
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException)

Example 30 with SystemException

use of javax.transaction.SystemException in project aries by apache.

the class TranStrategyTest method testRequiresNew_TmExceptions.

@Test
public void testRequiresNew_TmExceptions() throws Exception {
    int[] allStates = new int[] { Status.STATUS_COMMITTED, Status.STATUS_COMMITTING, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_PREPARED, Status.STATUS_PREPARING, Status.STATUS_ROLLEDBACK, Status.STATUS_MARKED_ROLLBACK, Status.STATUS_ROLLING_BACK, Status.STATUS_UNKNOWN };
    // SystemException and NotSupportedException from tm.begin()
    Set<Exception> ees = new HashSet<Exception>();
    ees.add(new SystemException("KABOOM!"));
    ees.add(new NotSupportedException("KABOOM!"));
    // from tm.begin()
    for (int i = 0; i < allStates.length; i++) {
        Iterator<Exception> iterator = ees.iterator();
        while (iterator.hasNext()) {
            Exception e = iterator.next();
            c.reset();
            expect(tm.getStatus()).andReturn(allStates[i]);
            expect(tm.getTransaction()).andReturn(null).anyTimes();
            tm.begin();
            expectLastCall().andThrow(e);
            requiresNewExceptionCheck(tm, allStates[i]);
        }
    }
}
Also used : SystemException(javax.transaction.SystemException) NotSupportedException(javax.transaction.NotSupportedException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

SystemException (javax.transaction.SystemException)102 Transaction (javax.transaction.Transaction)34 RollbackException (javax.transaction.RollbackException)29 NotSupportedException (javax.transaction.NotSupportedException)22 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)18 IOException (java.io.IOException)16 HeuristicMixedException (javax.transaction.HeuristicMixedException)16 UserTransaction (javax.transaction.UserTransaction)14 XAException (javax.transaction.xa.XAException)13 Test (org.junit.Test)12 TransactionManager (javax.transaction.TransactionManager)11 SQLException (java.sql.SQLException)10 LogWriterI18n (org.apache.geode.i18n.LogWriterI18n)10 InvalidTransactionException (javax.transaction.InvalidTransactionException)9 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)8 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)8 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 XAResource (javax.transaction.xa.XAResource)7 Synchronization (javax.transaction.Synchronization)6 File (java.io.File)5