Search in sources :

Example 16 with SystemException

use of javax.transaction.SystemException in project neo4j-mobile-android by neo4j-contrib.

the class TxManager method rollback.

public void rollback() throws IllegalStateException, SystemException {
    assertTmOk("tx rollback");
    Thread thread = Thread.currentThread();
    TransactionImpl tx = txThreadMap.get(thread);
    if (tx == null) {
        throw new IllegalStateException("Not in transaction");
    }
    boolean hasAnyLocks = false;
    try {
        hasAnyLocks = finishHook.hasAnyLocks(tx);
        if (tx.getStatus() == Status.STATUS_ACTIVE || tx.getStatus() == Status.STATUS_MARKED_ROLLBACK || tx.getStatus() == Status.STATUS_PREPARING) {
            tx.setStatus(Status.STATUS_MARKED_ROLLBACK);
            tx.doBeforeCompletion();
            // delist resources?
            try {
                rolledBackTxCount.incrementAndGet();
                tx.doRollback();
            } catch (XAException e) {
                log.log(Level.SEVERE, "Unable to rollback marked or active transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->", e);
                setTmNotOk(e);
                throw logAndReturn("TM error tx rollback", Exceptions.withCause(new SystemException("Unable to rollback " + " ---> error code for rollback: " + e.errorCode), e));
            }
            tx.doAfterCompletion();
            txThreadMap.remove(thread);
            try {
                if (tx.isGlobalStartRecordWritten()) {
                    getTxLog().txDone(tx.getGlobalId());
                }
            } catch (IOException e) {
                log.log(Level.SEVERE, "Error writing transaction log", e);
                setTmNotOk(e);
                throw logAndReturn("TM error tx rollback", Exceptions.withCause(new SystemException("TM encountered a problem, " + " error writing transaction log"), e));
            }
            tx.setStatus(Status.STATUS_NO_TRANSACTION);
        } else {
            throw new IllegalStateException("Tx status is: " + getTxStatusAsString(tx.getStatus()));
        }
    } finally {
        if (hasAnyLocks) {
            finishHook.finishTransaction(tx.getEventIdentifier(), false);
        }
    }
}
Also used : XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) IOException(java.io.IOException)

Example 17 with SystemException

use of javax.transaction.SystemException in project neo4j-mobile-android by neo4j-contrib.

the class PersistenceManager method delistResourcesForTransaction.

void delistResourcesForTransaction() throws NotInTransactionException {
    Transaction tx = this.getCurrentTransaction();
    if (tx == null) {
        throw new NotInTransactionException();
    }
    NeoStoreTransaction con = txConnectionMap.get(tx);
    if (con != null) {
        try {
            tx.delistResource(con.getXAResource(), XAResource.TMSUCCESS);
        } catch (SystemException e) {
            throw new TransactionFailureException("Failed to delist resource '" + con + "' from current transaction.", e);
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException)

Example 18 with SystemException

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

the class ManagedTransaction method commit.

@Override
public void commit() {
    if (initiatedTransaction) {
        try {
            transactionListener.beforeCommit(entities.types());
            getUserTransaction().commit();
            transactionListener.afterCommit(entities.types());
        } catch (RollbackException | SystemException | HeuristicMixedException | HeuristicRollbackException e) {
            throw new TransactionException(e);
        }
    }
    try {
        entities.clear();
    } finally {
        close();
    }
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) TransactionException(io.requery.TransactionException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException)

Example 19 with SystemException

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

the class ManagedTransaction method rollback.

@Override
public void rollback() {
    if (!rolledBack) {
        try {
            if (!completed) {
                transactionListener.beforeRollback(entities.types());
                if (initiatedTransaction) {
                    try {
                        getUserTransaction().rollback();
                    } catch (SystemException e) {
                        throw new TransactionException(e);
                    }
                } else if (active()) {
                    getSynchronizationRegistry().setRollbackOnly();
                }
                transactionListener.afterRollback(entities.types());
            }
        } finally {
            rolledBack = true;
            entities.clearAndInvalidate();
        }
    }
}
Also used : TransactionException(io.requery.TransactionException) SystemException(javax.transaction.SystemException)

Example 20 with SystemException

use of javax.transaction.SystemException in project spring-framework by spring-projects.

the class DataSourceJtaTransactionTests method doTestJtaTransactionWithPropagationRequiresNewAndBeginException.

private void doTestJtaTransactionWithPropagationRequiresNewAndBeginException(boolean suspendException, final boolean openOuterConnection, final boolean useTransactionAwareDataSource) throws Exception {
    given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    if (suspendException) {
        given(transactionManager.suspend()).willThrow(new SystemException());
    } else {
        given(transactionManager.suspend()).willReturn(transaction);
        willThrow(new SystemException()).given(userTransaction).begin();
    }
    given(connection.isReadOnly()).willReturn(true);
    final DataSource dsToUse = useTransactionAwareDataSource ? new TransactionAwareDataSourceProxy(dataSource) : dataSource;
    if (dsToUse instanceof TransactionAwareDataSourceProxy) {
        ((TransactionAwareDataSourceProxy) dsToUse).setReobtainTransactionalConnections(true);
    }
    JtaTransactionManager ptm = new JtaTransactionManager(userTransaction, transactionManager);
    final TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
                assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue("Is new transaction", status.isNewTransaction());
                Connection c = DataSourceUtils.getConnection(dsToUse);
                try {
                    assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
                    c.isReadOnly();
                    DataSourceUtils.releaseConnection(c, dsToUse);
                    c = DataSourceUtils.getConnection(dsToUse);
                    assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
                    if (!openOuterConnection) {
                        DataSourceUtils.releaseConnection(c, dsToUse);
                    }
                } catch (SQLException ex) {
                }
                try {
                    tt.execute(new TransactionCallbackWithoutResult() {

                        @Override
                        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                            assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
                            assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
                            assertTrue("Is new transaction", status.isNewTransaction());
                            Connection c = DataSourceUtils.getConnection(dsToUse);
                            assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
                            DataSourceUtils.releaseConnection(c, dsToUse);
                            c = DataSourceUtils.getConnection(dsToUse);
                            assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
                            DataSourceUtils.releaseConnection(c, dsToUse);
                        }
                    });
                } finally {
                    if (openOuterConnection) {
                        try {
                            c.isReadOnly();
                            DataSourceUtils.releaseConnection(c, dsToUse);
                        } catch (SQLException ex) {
                        }
                    }
                }
            }
        });
        fail("Should have thrown TransactionException");
    } catch (TransactionException ex) {
    // expected
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
    verify(userTransaction).begin();
    if (suspendException) {
        verify(userTransaction).rollback();
    }
    if (suspendException) {
        verify(connection, atLeastOnce()).close();
    } else {
        verify(connection, never()).close();
    }
}
Also used : JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionException(org.springframework.transaction.TransactionException) SystemException(javax.transaction.SystemException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) DataSource(javax.sql.DataSource)

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