Search in sources :

Example 61 with Transaction

use of javax.transaction.Transaction 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 62 with Transaction

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

the class SimpleTransactionSynchronizationRegistry method getActiveTransaction.

private Transaction getActiveTransaction() {
    try {
        final Transaction transaction = transactionManager.getTransaction();
        if (transaction == null) {
            throw new IllegalStateException("No transaction active");
        }
        final int status = transaction.getStatus();
        if (status != Status.STATUS_ACTIVE && status != Status.STATUS_MARKED_ROLLBACK) {
            throw new IllegalStateException("No transaction active");
        }
        return transaction;
    } catch (final SystemException e) {
        throw new IllegalStateException("No transaction active", e);
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException)

Example 63 with Transaction

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

the class TxBeanManaged method commit.

public void commit() throws ApplicationException, SystemException {
    try {
        // The Container must detect the case in which a transaction was started, but
        // not completed, in the business method, and handle it as follows:
        final Transaction currentTx = getTransaction();
        if (currentTx != null) {
            final String message = "The EJB started a transaction but did not complete it.";
            /* [1] Log this as an application error ********/
            logger.error(message);
            /* [2] Roll back the started transaction *******/
            try {
                rollbackTransaction(currentTx);
            } catch (final Throwable t) {
            // no-op
            }
            /* [3] Throw the RemoteException to the client */
            throw new ApplicationException(new RemoteException(message));
        }
        fireNonTransactionalCompletion();
    } finally {
        resumeTransaction(clientTx);
    }
}
Also used : ApplicationException(org.apache.openejb.ApplicationException) Transaction(javax.transaction.Transaction) UserTransaction(javax.transaction.UserTransaction) CoreUserTransaction(org.apache.openejb.core.CoreUserTransaction) RemoteException(java.rmi.RemoteException)

Example 64 with Transaction

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

the class JtaTransactionPolicy method beginTransaction.

protected Transaction beginTransaction() throws SystemException {
    final Transaction transaction;
    try {
        transactionManager.begin();
        transaction = transactionManager.getTransaction();
    } catch (final Exception e) {
        txLogger.error("The Transaction Manager has encountered an unexpected error condition while attempting to begin a new transaction: {0}", e.getMessage());
        throw new SystemException(e);
    }
    if (transaction == null) {
        throw new SystemException("Failed to begin a new transaction");
    }
    txLogger.debug("TX {0}: Started transaction {1}", transactionType, transaction);
    return transaction;
}
Also used : Transaction(javax.transaction.Transaction) SystemException(org.apache.openejb.SystemException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) InvalidTransactionException(javax.transaction.InvalidTransactionException) RemoteException(java.rmi.RemoteException) SystemException(org.apache.openejb.SystemException) RollbackException(javax.transaction.RollbackException) ApplicationException(org.apache.openejb.ApplicationException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 65 with Transaction

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

the class TimerData method registerTimerDataSynchronization.

private void registerTimerDataSynchronization() throws TimerStoreException {
    if (synchronizationRegistered) {
        return;
    }
    try {
        final Transaction transaction = timerService.getTransactionManager().getTransaction();
        final int status = transaction == null ? Status.STATUS_NO_TRANSACTION : transaction.getStatus();
        if (transaction != null && status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK) {
            transaction.registerSynchronization(new TimerDataSynchronization());
            synchronizationRegistered = true;
            return;
        }
    } catch (final Exception e) {
        log.warning("Unable to register timer data transaction synchronization", e);
    }
    // there either wasn't a transaction or registration failed... call transactionComplete directly
    transactionComplete(true);
}
Also used : Transaction(javax.transaction.Transaction) IOException(java.io.IOException) EJBException(javax.ejb.EJBException) SchedulerException(org.apache.openejb.quartz.SchedulerException)

Aggregations

Transaction (javax.transaction.Transaction)160 SystemException (javax.transaction.SystemException)55 Test (org.junit.Test)42 RollbackException (javax.transaction.RollbackException)26 TransactionManager (javax.transaction.TransactionManager)24 UserTransaction (javax.transaction.UserTransaction)19 NotInTransactionException (org.neo4j.graphdb.NotInTransactionException)14 NotSupportedException (javax.transaction.NotSupportedException)13 Synchronization (javax.transaction.Synchronization)10 XAResource (javax.transaction.xa.XAResource)10 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)10 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)8 InvalidTransactionException (javax.transaction.InvalidTransactionException)7 TransactionContext (com.hazelcast.transaction.TransactionContext)6 RemoteException (java.rmi.RemoteException)6 ResourceException (javax.resource.ResourceException)6 ManagedConnection (javax.resource.spi.ManagedConnection)6 SQLException (java.sql.SQLException)5 HeuristicMixedException (javax.transaction.HeuristicMixedException)5 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)5