Search in sources :

Example 11 with TransactionException

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

the class JtaTransactionAdapterUserTransactionImpl method rollback.

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

Example 12 with TransactionException

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

the class JtaTransactionAdapterUserTransactionImpl method begin.

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

Example 13 with TransactionException

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

the class FooBarTest method testLazyCollectionsTouchedDuringPreCommit.

@Test
@TestForIssue(jiraKey = "HHH-7603")
public void testLazyCollectionsTouchedDuringPreCommit() throws Exception {
    Session s = openSession();
    s.beginTransaction();
    Qux q = new Qux();
    s.save(q);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    q = (Qux) s.load(Qux.class, q.getKey());
    s.getTransaction().commit();
    // clear the session
    s.clear();
    // now reload the proxy and delete it
    s.beginTransaction();
    final Qux qToDelete = (Qux) s.load(Qux.class, q.getKey());
    // register a pre commit process that will touch the collection and delete the entity
    ((EventSource) s).getActionQueue().registerProcess(new BeforeTransactionCompletionProcess() {

        @Override
        public void doBeforeTransactionCompletion(SessionImplementor session) {
            qToDelete.getFums().size();
        }
    });
    s.delete(qToDelete);
    boolean ok = false;
    try {
        s.getTransaction().commit();
    } catch (LazyInitializationException e) {
        ok = true;
        s.getTransaction().rollback();
    } catch (TransactionException te) {
        if (te.getCause() instanceof LazyInitializationException) {
            ok = true;
        }
        s.getTransaction().rollback();
    } finally {
        s.close();
    }
    assertTrue("lazy collection should have blown in the before trans completion", ok);
    s = openSession();
    s.beginTransaction();
    q = (Qux) s.load(Qux.class, q.getKey());
    s.delete(q);
    s.getTransaction().commit();
    s.close();
}
Also used : BeforeTransactionCompletionProcess(org.hibernate.action.spi.BeforeTransactionCompletionProcess) TransactionException(org.hibernate.TransactionException) LazyInitializationException(org.hibernate.LazyInitializationException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 14 with TransactionException

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

the class BasicJdbcTransactionTests method testMarkRollbackOnly.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void testMarkRollbackOnly() {
    try (final SessionFactoryImplementor sf = generateSessionFactory()) {
        inSession(sf, session -> {
            final TransactionCoordinator coordinator = session.getTransactionCoordinator();
            assertEquals(TransactionStatus.NOT_ACTIVE, coordinator.getTransactionDriverControl().getStatus());
            session.getTransaction().begin();
            assertEquals(TransactionStatus.ACTIVE, coordinator.getTransactionDriverControl().getStatus());
            session.getTransaction().markRollbackOnly();
            assertEquals(TransactionStatus.MARKED_ROLLBACK, coordinator.getTransactionDriverControl().getStatus());
            try {
                session.getTransaction().commit();
            } catch (TransactionException expected) {
            } finally {
                assertThat(coordinator.getTransactionDriverControl().getStatus(), anyOf(is(TransactionStatus.NOT_ACTIVE), is(TransactionStatus.ROLLED_BACK)));
            }
        });
    }
}
Also used : TransactionException(org.hibernate.TransactionException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) Test(org.junit.Test)

Example 15 with TransactionException

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

the class AbstractLogicalConnectionImplementor method begin.

@Override
public void begin() {
    try {
        if (!doConnectionsFromProviderHaveAutoCommitDisabled()) {
            log.trace("Preparing to begin transaction via JDBC Connection.setAutoCommit(false)");
            getConnectionForTransactionManagement().setAutoCommit(false);
            log.trace("Transaction begun via JDBC Connection.setAutoCommit(false)");
        }
        status = TransactionStatus.ACTIVE;
    } catch (SQLException e) {
        throw new TransactionException("JDBC begin transaction failed: ", e);
    }
}
Also used : TransactionException(org.hibernate.TransactionException) SQLException(java.sql.SQLException)

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