Search in sources :

Example 16 with RollbackException

use of javax.transaction.RollbackException in project wildfly by wildfly.

the class TimerServiceImpl method registerSynchronization.

private void registerSynchronization(Synchronization synchronization) {
    try {
        final Transaction tx = this.getTransaction();
        // register for lifecycle events of transaction
        tx.registerSynchronization(synchronization);
    } catch (RollbackException e) {
        throw new EJBException(e);
    } catch (SystemException e) {
        throw new EJBException(e);
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) EJBException(javax.ejb.EJBException)

Example 17 with RollbackException

use of javax.transaction.RollbackException in project wildfly by wildfly.

the class DatabaseTimerPersistence method shouldRun.

@Override
public boolean shouldRun(TimerImpl timer, TransactionManager tm) {
    if (!allowExecution) {
        //timers never execute on this node
        return false;
    }
    String loadTimer = sql(UPDATE_RUNNING);
    Connection connection = null;
    PreparedStatement statement = null;
    try {
        try {
            connection = dataSource.getConnection();
            statement = connection.prepareStatement(loadTimer);
            statement.setString(1, TimerState.IN_TIMEOUT.name());
            setNodeName(TimerState.IN_TIMEOUT, statement, 2);
            statement.setString(3, timer.getId());
            statement.setString(4, TimerState.IN_TIMEOUT.name());
            statement.setString(5, TimerState.RETRY_TIMEOUT.name());
            if (timer.getNextExpiration() == null) {
                statement.setTimestamp(6, null);
            } else {
                statement.setTimestamp(6, timestamp(timer.getNextExpiration()));
            }
        } catch (SQLException e) {
            // something wrong with the preparation
            throw new RuntimeException(e);
        }
        tm.begin();
        int affected = statement.executeUpdate();
        tm.commit();
        return affected == 1;
    } catch (SQLException | SystemException | SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
        // failed to update the DB
        try {
            tm.rollback();
        } catch (IllegalStateException | SecurityException | SystemException rbe) {
            EjbLogger.EJB3_TIMER_LOGGER.timerUpdateFailedAndRollbackNotPossible(rbe);
        }
        EjbLogger.EJB3_TIMER_LOGGER.debugf(e, "Timer %s not running due to exception ", timer);
        return false;
    } catch (NotSupportedException e) {
        // happen from tm.begin, no rollback necessary
        EjbLogger.EJB3_TIMER_LOGGER.timerNotRunning(e, timer);
        return false;
    } finally {
        safeClose(statement);
        safeClose(connection);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException)

Example 18 with RollbackException

use of javax.transaction.RollbackException in project wildfly by wildfly.

the class InfinispanBatcher method createBatch.

@Override
public TransactionBatch createBatch() {
    if (this.tm == null)
        return NON_TX_BATCH;
    TransactionBatch batch = CURRENT_BATCH.get();
    if (batch != null) {
        return batch.interpose();
    }
    try {
        this.tm.suspend();
        this.tm.begin();
        Transaction tx = this.tm.getTransaction();
        tx.registerSynchronization(CURRENT_BATCH_REMOVER);
        batch = new InfinispanBatch(tx);
        CURRENT_BATCH.set(batch);
        return batch;
    } catch (RollbackException | SystemException | NotSupportedException e) {
        throw new CacheException(e);
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) CacheException(org.infinispan.commons.CacheException) RollbackException(javax.transaction.RollbackException) NotSupportedException(javax.transaction.NotSupportedException)

Example 19 with RollbackException

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

the class ManagedConnection method invoke.

@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    // first some Object method management
    final String mtdName = method.getName();
    if ("toString".equals(mtdName)) {
        return "ManagedConnection{" + delegate + "}";
    }
    if ("hashCode".equals(mtdName)) {
        return hashCode();
    }
    if ("equals".equals(mtdName)) {
        InvocationHandler handler;
        return args[0] == this || ((handler = unwrapHandler(args[0])) == this) || (delegate != null && delegate.equals(unwrapDelegate(args[0], handler)));
    }
    // allow to get delegate if needed by the underlying program
    if (Wrapper.class == method.getDeclaringClass() && args.length == 1 && Connection.class == args[0]) {
        if ("isWrapperFor".equals(mtdName)) {
            return true;
        }
        if ("unwrap".equals(mtdName)) {
            return delegate;
        }
    }
    // here the real logic starts
    try {
        final Transaction transaction = transactionManager.getTransaction();
        // shouldn't be used without a transaction but if so just delegate to the actual connection
        if (transaction == null) {
            if ("close".equals(mtdName)) {
                if (delegate == null) {
                    // no need to get a connection
                    return close();
                }
                closeConnection(true);
                return null;
            }
            if ("isClosed".equals(mtdName) && closed) {
                return true;
            }
            if (delegate == null) {
                newConnection();
            }
            return invoke(method, delegate, args);
        }
        // if we have a tx check it is the same this connection is linked to
        if (currentTransaction != null && isUnderTransaction(currentTransaction.getStatus())) {
            if (!currentTransaction.equals(transaction)) {
                throw new SQLException("Connection can not be used while enlisted in another transaction");
            }
            return invokeUnderTransaction(method, args);
        }
        // get the already bound connection to the current transaction or enlist this one in the tx
        final int transactionStatus = transaction.getStatus();
        if (isUnderTransaction(transactionStatus)) {
            Connection connection = Connection.class.cast(registry.getResource(key));
            if (connection == null && delegate == null) {
                newConnection();
                currentTransaction = transaction;
                try {
                    if (!transaction.enlistResource(getXAResource())) {
                        closeConnection(true);
                        throw new SQLException("Unable to enlist connection in transaction: enlistResource returns 'false'.");
                    }
                } catch (final RollbackException ignored) {
                // no-op
                } catch (final SystemException e) {
                    throw new SQLException("Unable to enlist connection the transaction", e);
                }
                registry.putResource(key, delegate);
                transaction.registerSynchronization(new ClosingSynchronization());
                if (xaConnection == null) {
                    try {
                        setAutoCommit(false);
                    } catch (final SQLException xae) {
                        // we are alreay in a transaction so this can't be called from a user perspective - some XA DataSource prevents it in their code
                        final String message = "Can't set auto commit to false cause the XA datasource doesn't support it, this is likely an issue";
                        final Logger logger = Logger.getInstance(LogCategory.OPENEJB_RESOURCE_JDBC, ManagedConnection.class);
                        if (logger.isDebugEnabled()) {
                            // we don't want to print the exception by default
                            logger.warning(message, xae);
                        } else {
                            logger.warning(message);
                        }
                    }
                }
            } else if (delegate == null) {
                // shouldn't happen
                delegate = connection;
            }
            return invokeUnderTransaction(method, args);
        }
        if ("isClosed".equals(mtdName) && closed) {
            return true;
        }
        if ("close".equals(mtdName)) {
            // let it be handled by the ClosingSynchronisation since we have a tx there
            return close();
        }
        // we shouldn't come here, tempted to just throw an exception
        if (delegate == null) {
            newConnection();
        }
        return invoke(method, delegate, args);
    } catch (final InvocationTargetException ite) {
        throw ite.getTargetException();
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) RollbackException(javax.transaction.RollbackException) Logger(org.apache.openejb.util.Logger) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 20 with RollbackException

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

the class SingletonBeanTxTests method BUG_test06_singleTransactionRollback.

/**
     * This test does work for the IntraVM Server, but it fails on
     * the Remote Server.  For some reason, when the RollbackException is
     * sent to the client, the server blocks.
     */
public void BUG_test06_singleTransactionRollback() {
    final Account expected = new Account("234-56-7890", "Charlie", "Brown", 20000);
    final Account actual = new Account();
    // throw a RollbackException
    try {
        ejbObject.openAccount(expected, Boolean.TRUE);
        fail("A javax.transaction.RollbackException should have been thrown.");
    } catch (final RollbackException re) {
    // Good.
    } catch (final Exception e) {
        fail("Received Exception " + e.getClass() + " : " + e.getMessage());
    }
//// Now check that the account really wasn't added.
//try{
//    actual = ejbObject.retreiveAccount( expected.getSsn() );
//    //assertTrue( "The transaction was commited when it should have been rolledback.", !expected.equals(actual) );
//} catch (Exception e){
//    fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
//}
}
Also used : Account(org.apache.openejb.test.object.Account) RollbackException(javax.transaction.RollbackException) RollbackException(javax.transaction.RollbackException)

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