Search in sources :

Example 1 with TransactionException

use of org.hibernate.TransactionException in project hibernate-orm by hibernate.

the class TransactionTimeoutTest method testTransactionTimeoutFailure.

@Test
public void testTransactionTimeoutFailure() throws InterruptedException {
    Session session = openSession();
    try {
        Transaction transaction = session.getTransaction();
        transaction.setTimeout(1);
        assertEquals(-1, ((SessionImplementor) session).getJdbcCoordinator().determineRemainingTransactionTimeOutPeriod());
        transaction.begin();
        Thread.sleep(1000);
        session.persist(new Person("Lukasz", "Antoniak"));
        transaction.commit();
    } catch (TransactionException e) {
    // expected
    } catch (PersistenceException e) {
        assertTyping(TransactionException.class, e.getCause());
    } finally {
        session.close();
    }
}
Also used : TransactionException(org.hibernate.TransactionException) Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Person(org.hibernate.test.jdbc.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with TransactionException

use of org.hibernate.TransactionException in project hibernate-orm by hibernate.

the class InterceptorTest method testPrepareStatementFaultIntercept.

public void testPrepareStatementFaultIntercept() {
    final Interceptor interceptor = new EmptyInterceptor() {

        @Override
        public String onPrepareStatement(String sql) {
            return null;
        }
    };
    Session s = openSession(interceptor);
    try {
        Transaction t = s.beginTransaction();
        User u = new User("Kinga", "Mroz");
        s.persist(u);
        t.commit();
    } catch (TransactionException e) {
        assertTrue(e.getCause() instanceof AssertionFailure);
    } finally {
        s.close();
    }
}
Also used : TransactionException(org.hibernate.TransactionException) AssertionFailure(org.hibernate.AssertionFailure) Transaction(org.hibernate.Transaction) EmptyInterceptor(org.hibernate.EmptyInterceptor) Interceptor(org.hibernate.Interceptor) EmptyInterceptor(org.hibernate.EmptyInterceptor) Session(org.hibernate.Session)

Example 3 with TransactionException

use of org.hibernate.TransactionException in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method testMarkRollbackOnly.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void testMarkRollbackOnly() throws Exception {
    JtaTransactionCoordinatorImpl transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, JtaPlatformStandardTestingImpl.INSTANCE, preferUserTransactions(), true);
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    transactionCoordinator.getTransactionDriverControl().begin();
    assertEquals(TransactionStatus.ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    transactionCoordinator.getTransactionDriverControl().markRollbackOnly();
    assertEquals(TransactionStatus.MARKED_ROLLBACK, transactionCoordinator.getTransactionDriverControl().getStatus());
    try {
        transactionCoordinator.getTransactionDriverControl().commit();
    } catch (TransactionException expected) {
    } finally {
        assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    }
}
Also used : TransactionException(org.hibernate.TransactionException) TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 4 with TransactionException

use of org.hibernate.TransactionException in project hibernate-orm by hibernate.

the class JtaTransactionAdapterTransactionManagerImpl method rollback.

@Override
public void rollback() {
    try {
        if (initiator) {
            initiator = false;
            log.trace("Calling TransactionManager#rollback");
            transactionManager.rollback();
            log.trace("Called TransactionManager#rollback");
        } else {
            markRollbackOnly();
        }
    } catch (Exception e) {
        throw new TransactionException("JTA TransactionManager#rollback failed", e);
    }
}
Also used : TransactionException(org.hibernate.TransactionException) SystemException(javax.transaction.SystemException) TransactionException(org.hibernate.TransactionException)

Example 5 with TransactionException

use of org.hibernate.TransactionException in project hibernate-orm by hibernate.

the class JtaTransactionAdapterUserTransactionImpl method commit.

@Override
public void commit() {
    try {
        if (initiator) {
            initiator = false;
            log.trace("Calling UserTransaction#commit");
            userTransaction.commit();
            log.trace("Called UserTransaction#commit");
        } else {
            log.trace("Skipping TransactionManager#commit due to not being initiator");
        }
    } catch (Exception e) {
        throw new TransactionException("JTA UserTransaction#commit failed", e);
    }
}
Also used : TransactionException(org.hibernate.TransactionException) SystemException(javax.transaction.SystemException) TransactionException(org.hibernate.TransactionException)

Aggregations

TransactionException (org.hibernate.TransactionException)16 SystemException (javax.transaction.SystemException)6 Test (org.junit.Test)5 Session (org.hibernate.Session)4 SQLException (java.sql.SQLException)3 Transaction (org.hibernate.Transaction)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 FutureTask (java.util.concurrent.FutureTask)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 PersistenceException (javax.persistence.PersistenceException)1 Query (javax.persistence.Query)1 TransactionManager (javax.transaction.TransactionManager)1 AssertionFailure (org.hibernate.AssertionFailure)1 EmptyInterceptor (org.hibernate.EmptyInterceptor)1 Interceptor (org.hibernate.Interceptor)1 LazyInitializationException (org.hibernate.LazyInitializationException)1 BeforeTransactionCompletionProcess (org.hibernate.action.spi.BeforeTransactionCompletionProcess)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1