Search in sources :

Example 86 with SystemException

use of javax.transaction.SystemException 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 87 with SystemException

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

the class Recovery method recover.

public static boolean recover(final String name, final XADataSource dataSource, final RecoverableTransactionManager transactionManager) throws IOException {
    if (name != null && name.length() > 0) {
        transactionManager.registerNamedXAResourceFactory(new NamedXAResourceFactory() {

            public String getName() {
                return name;
            }

            public NamedXAResource getNamedXAResource() throws SystemException {
                try {
                    final XAConnection connection = dataSource.getXAConnection();
                    LOGGER.debug("new namedXAResource's connection: " + connection);
                    return new ConnectionAndWrapperNamedXAResource(connection.getXAResource(), getName(), connection);
                } catch (Exception e) {
                    SystemException se = new SystemException("Failed to create ConnectionAndWrapperNamedXAResource, " + e.getLocalizedMessage());
                    se.initCause(e);
                    LOGGER.error(se.getLocalizedMessage(), se);
                    throw se;
                }
            }

            public void returnNamedXAResource(NamedXAResource namedXaResource) {
                if (namedXaResource instanceof ConnectionAndWrapperNamedXAResource) {
                    try {
                        LOGGER.debug("closing returned namedXAResource's connection: " + ((ConnectionAndWrapperNamedXAResource) namedXaResource).connection);
                        ((ConnectionAndWrapperNamedXAResource) namedXaResource).connection.close();
                    } catch (Exception ignored) {
                        LOGGER.debug("failed to close returned namedXAResource: " + namedXaResource, ignored);
                    }
                }
            }
        });
        return true;
    } else {
        LOGGER.warn("Unable to recover XADataSource: aries.xa.name property not set");
        return false;
    }
}
Also used : SystemException(javax.transaction.SystemException) WrapperNamedXAResource(org.apache.geronimo.transaction.manager.WrapperNamedXAResource) NamedXAResource(org.apache.geronimo.transaction.manager.NamedXAResource) SystemException(javax.transaction.SystemException) IOException(java.io.IOException) NamedXAResourceFactory(org.apache.geronimo.transaction.manager.NamedXAResourceFactory) XAConnection(javax.sql.XAConnection)

Example 88 with SystemException

use of javax.transaction.SystemException 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 89 with SystemException

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

the class JCALocalTransaction method begin.

@Override
public void begin() throws ResourceException {
    try {
        if (!this.initDone || this.cache.isClosed()) {
            this.init();
        }
        LogWriter logger = this.cache.getLogger();
        if (logger.fineEnabled()) {
            logger.fine("JCALocalTransaction::begin:");
        }
        TransactionManager tm = this.cache.getJTATransactionManager();
        if (this.tid != null) {
            throw new LocalTransactionException(" A transaction is already in progress");
        }
        if (tm != null && tm.getTransaction() != null) {
            if (logger.fineEnabled()) {
                logger.fine("JCAManagedConnection: JTA transaction is on");
            }
            // This is having a JTA transaction. Assuming ignore jta flag is true,
            // explicitly being a gemfire transaction.
            TXStateProxy tsp = this.gfTxMgr.getTXState();
            if (tsp == null) {
                this.gfTxMgr.begin();
                tsp = this.gfTxMgr.getTXState();
                tsp.setJCATransaction();
                this.tid = tsp.getTransactionId();
                if (logger.fineEnabled()) {
                    logger.fine("JCALocalTransaction:begun GFE transaction");
                }
            } else {
                throw new LocalTransactionException("GemFire is already associated with a transaction");
            }
        } else {
            if (logger.fineEnabled()) {
                logger.fine("JCAManagedConnection: JTA Transaction does not exist.");
            }
        }
    } catch (SystemException e) {
        throw new ResourceException(e);
    }
}
Also used : LocalTransactionException(javax.resource.spi.LocalTransactionException) SystemException(javax.transaction.SystemException) LogWriter(org.apache.geode.LogWriter) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) TransactionManager(javax.transaction.TransactionManager) ResourceException(javax.resource.ResourceException)

Example 90 with SystemException

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

the class LocalRegion method getJTAEnlistedTX.

private TXStateInterface getJTAEnlistedTX() {
    if (this.ignoreJTA) {
        // fixes bug 45541
        return null;
    }
    TXStateInterface txState = getTXState();
    if (txState != null) {
        return txState;
    } else {
        try {
            if (!this.ignoreJTA && this.cache.getJTATransactionManager() != null) {
                Transaction jtaTransaction = this.cache.getJTATransactionManager().getTransaction();
                if (jtaTransaction == null || jtaTransaction.getStatus() == Status.STATUS_NO_TRANSACTION) {
                    return null;
                }
                txState = this.cache.getTXMgr().beginJTA();
                jtaTransaction.registerSynchronization(txState);
                return txState;
            } else {
                return null;
            }
        } catch (SystemException se) {
            // this can be thrown when the system is shutting down (see bug #39728)
            this.stopper.checkCancelInProgress(se);
            jtaEnlistmentFailureCleanup(txState, se);
            return null;
        } catch (RollbackException | IllegalStateException re) {
            jtaEnlistmentFailureCleanup(txState, re);
            return null;
        }
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException)

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