Search in sources :

Example 36 with RollbackException

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

the class TxDBServlet method insertIntoTransaction.

/**
	 * This method demonstrates how to enlist JDBC connection into Transaction according OSGi enterprise specification.
	 * 
	 * @param xads XADataSource
	 * @param tm TransactionManager
	 * @param value which will be inserted into table
	 * @param toCommit Specify if the transaction will be committed or rolledback
	 * @throws SQLException
	 * @throws GenericJTAException
	 */
private void insertIntoTransaction(XADataSource xads, TransactionManager tm, String value, boolean toCommit) throws SQLException, GenericJTAException {
    XAConnection xaConnection = xads.getXAConnection();
    Connection connection = xaConnection.getConnection();
    XAResource xaResource = xaConnection.getXAResource();
    try {
        tm.begin();
        Transaction transaction = tm.getTransaction();
        transaction.enlistResource(xaResource);
        PreparedStatement insertStatement = connection.prepareStatement(INSERT_INTO_TABLE);
        insertStatement.setString(1, value);
        insertStatement.executeUpdate();
        if (toCommit) {
            transaction.commit();
        } else {
            transaction.rollback();
        }
    } catch (RollbackException e) {
        throw new GenericJTAException(e);
    } catch (SecurityException e) {
        throw new GenericJTAException(e);
    } catch (IllegalStateException e) {
        throw new GenericJTAException(e);
    } catch (HeuristicMixedException e) {
        throw new GenericJTAException(e);
    } catch (HeuristicRollbackException e) {
        throw new GenericJTAException(e);
    } catch (SystemException e) {
        throw new GenericJTAException(e);
    } catch (NotSupportedException e) {
        throw new GenericJTAException(e);
    }
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PreparedStatement(java.sql.PreparedStatement) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) XAResource(javax.transaction.xa.XAResource) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException) XAConnection(javax.sql.XAConnection)

Example 37 with RollbackException

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

the class TxInterceptorImpl method postCallWithReturn.

@Override
public void postCallWithReturn(ComponentMetadata cm, Method m, Object returnType, Object preCallToken) throws Exception {
    LOGGER.debug("PostCallWithReturn for bean {}, method {}.", getCmId(cm), m);
    // it is possible transaction is not involved at all
    if (preCallToken == null) {
        return;
    }
    if (!(preCallToken instanceof TransactionToken)) {
        throw new IllegalStateException("Expected a TransactionToken from preCall but got " + preCallToken);
    }
    final TransactionToken token = (TransactionToken) preCallToken;
    safeEndCoordination(token);
    try {
        token.getTransactionAttribute().finish(tm, token);
    } catch (Exception e) {
        // We are throwing an exception, so we don't error it out
        LOGGER.debug("Exception while completing transaction.", e);
        RollbackException rbe = new javax.transaction.RollbackException();
        rbe.addSuppressed(e);
        throw rbe;
    }
}
Also used : RollbackException(javax.transaction.RollbackException) RollbackException(javax.transaction.RollbackException) RollbackException(javax.transaction.RollbackException)

Example 38 with RollbackException

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

the class AbstractIntegrationTest method assertInsertWithRuntimeExceptionRolledBack.

// Test with client transaction and runtime exception - the user transaction is rolled back
protected void assertInsertWithRuntimeExceptionRolledBack() throws Exception {
    TestBean bean = getBean();
    int initialRows = counter.countRows();
    if (clientTransaction) {
        tran.begin();
    }
    bean.insertRow("testWithClientTranAndWithRuntimeException", 1, null);
    try {
        bean.insertRow("testWithClientTranAndWithRuntimeException", 2, new RuntimeException("Dummy exception"));
    } catch (RuntimeException e) {
        Assert.assertEquals("Dummy exception", e.getMessage());
    }
    if (clientTransaction) {
        try {
            tran.commit();
            fail("RollbackException not thrown");
        } catch (RollbackException e) {
        // Ignore expected
        }
    }
    int finalRows = counter.countRows();
    // In case of client transaction both are rolled back
    // In case of container transaction only second insert is rolled back
    assertEquals("Added rows", clientTransaction ? 0 : 1, finalRows - initialRows);
}
Also used : TestBean(org.apache.aries.transaction.test.TestBean) RollbackException(javax.transaction.RollbackException)

Example 39 with RollbackException

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

the class XaConnectionPool method createSession.

@Override
public Session createSession(boolean transacted, int ackMode) throws JMSException {
    try {
        boolean isXa = (transactionManager != null && transactionManager.getStatus() != Status.STATUS_NO_TRANSACTION);
        if (isXa) {
            // if the xa tx aborts inflight we don't want to auto create a
            // local transaction or auto ack
            transacted = false;
            ackMode = Session.CLIENT_ACKNOWLEDGE;
        } else if (transactionManager != null) {
            // cmt or transactionManager managed
            transacted = false;
            if (ackMode == Session.SESSION_TRANSACTED) {
                ackMode = Session.AUTO_ACKNOWLEDGE;
            }
        }
        PooledSession session = (PooledSession) super.createSession(transacted, ackMode);
        if (isXa) {
            session.addSessionEventListener(new PooledSessionEventListener() {

                @Override
                public void onTemporaryQueueCreate(TemporaryQueue tempQueue) {
                }

                @Override
                public void onTemporaryTopicCreate(TemporaryTopic tempTopic) {
                }

                @Override
                public void onSessionClosed(PooledSession session) {
                    session.setIgnoreClose(true);
                    session.setIsXa(false);
                }
            });
            session.setIgnoreClose(true);
            session.setIsXa(true);
            transactionManager.getTransaction().registerSynchronization(new Synchronization(session));
            incrementReferenceCount();
            transactionManager.getTransaction().enlistResource(createXaResource(session));
        } else {
            session.setIgnoreClose(false);
        }
        return session;
    } catch (RollbackException e) {
        final JMSException jmsException = new JMSException("Rollback Exception");
        jmsException.initCause(e);
        throw jmsException;
    } catch (SystemException e) {
        final JMSException jmsException = new JMSException("System Exception");
        jmsException.initCause(e);
        throw jmsException;
    }
}
Also used : SystemException(javax.transaction.SystemException) TemporaryQueue(javax.jms.TemporaryQueue) JMSException(javax.jms.JMSException) RollbackException(javax.transaction.RollbackException) TemporaryTopic(javax.jms.TemporaryTopic)

Example 40 with RollbackException

use of javax.transaction.RollbackException in project geode by apache.

the class TXWriterJUnitTest method testAfterCommitFailedOnTransactionWriterThrowWithJTA.

/**
   * make sure standard Cache(Listener,Writer) are not called during rollback due to transaction
   * writer throw
   */
@Test
public void testAfterCommitFailedOnTransactionWriterThrowWithJTA() throws Exception {
    installCacheListenerAndWriter();
    ((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {

        public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
            throw new TransactionWriterException("Rollback now!");
        }

        public void close() {
        }
    });
    installTransactionListener();
    UserTransaction userTx = (UserTransaction) this.cache.getJNDIContext().lookup("java:/UserTransaction");
    userTx.begin();
    this.region.create("key1", "value1");
    this.cbCount = 0;
    try {
        userTx.commit();
        fail("Commit should have thrown RollbackException");
    } catch (RollbackException expected) {
        assertNotNull(expected.getCause());
        assertTrue(expected.getCause() + " is not a SynchronizationCommitConflictException", expected.getCause() instanceof SynchronizationCommitConflictException);
    }
    assertEquals(0, this.cbCount);
    assertEquals(1, this.failedCommits);
    assertEquals(0, this.afterCommits);
    assertEquals(1, this.afterRollbacks);
}
Also used : UserTransaction(javax.transaction.UserTransaction) TransactionEvent(org.apache.geode.cache.TransactionEvent) TransactionWriter(org.apache.geode.cache.TransactionWriter) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) SynchronizationCommitConflictException(org.apache.geode.cache.SynchronizationCommitConflictException) RollbackException(javax.transaction.RollbackException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

RollbackException (javax.transaction.RollbackException)57 SystemException (javax.transaction.SystemException)25 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)15 Transaction (javax.transaction.Transaction)14 UserTransaction (javax.transaction.UserTransaction)14 HeuristicMixedException (javax.transaction.HeuristicMixedException)12 Test (org.junit.Test)11 Connection (java.sql.Connection)8 XAException (javax.transaction.xa.XAException)8 PreparedStatement (java.sql.PreparedStatement)7 NotSupportedException (javax.transaction.NotSupportedException)7 IOException (java.io.IOException)6 EJBException (javax.ejb.EJBException)6 Account (org.apache.openejb.test.object.Account)6 RemoteException (java.rmi.RemoteException)5 CreateException (javax.ejb.CreateException)5 DataSource (javax.sql.DataSource)5 SQLException (java.sql.SQLException)4 Synchronization (javax.transaction.Synchronization)4 XAResource (javax.transaction.xa.XAResource)4