Search in sources :

Example 1 with ConnectionModifier

use of com.arjuna.ats.internal.jdbc.drivers.modifiers.ConnectionModifier in project narayana by jbosstm.

the class ConnectionImple method registerDatabase.

/**
 * Whenever a JDBC call is invoked on us we get an XAResource and try to
 * register it with the transaction. If the same thread causes this to
 * happen many times within the same transaction then we will currently
 * attempt to get and register many redundant XAResources for it. The JTA
 * implementation will detect this and ignore all but the first for each
 * thread. However, a further optimisation would be to trap such calls here
 * and not do a registration at all. This would require the connection
 * object to be informed whenever a transaction completes so that it could
 * flush its cache of XAResources though.
 */
protected final synchronized void registerDatabase() throws SQLException {
    if (jdbcLogger.logger.isTraceEnabled()) {
        jdbcLogger.logger.trace("ConnectionImple.registerDatabase ()");
    }
    boolean needsClose = _theConnection == null;
    Connection theConnection = getConnection();
    if (theConnection != null) {
        XAResource xares = null;
        try {
            javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
            javax.transaction.Transaction tx = tm.getTransaction();
            if (tx == null) {
                return;
            }
            if (!_transactionalDriverXAConnectionConnection.setTransaction(tx))
                throw new SQLException(jdbcLogger.i18NLogger.get_alreadyassociated());
            Object[] params;
            if (_theModifier != null)
                params = new Object[2];
            else
                params = new Object[1];
            params[com.arjuna.ats.jta.transaction.Transaction.XACONNECTION] = _transactionalDriverXAConnectionConnection;
            if (_theModifier != null)
                params[com.arjuna.ats.jta.transaction.Transaction.XAMODIFIER] = (XAModifier) _theModifier;
            /*
				 * Use our extended version of enlistResource.
				 */
            xares = _transactionalDriverXAConnectionConnection.getResource();
            if (!((com.arjuna.ats.jta.transaction.Transaction) tx).enlistResource(xares, params)) {
                try {
                    tx.setRollbackOnly();
                } catch (Exception e) {
                    jdbcLogger.i18NLogger.warn_rollbackerror("ConnectionImple.registerDatabase");
                    SQLException sqlException = new SQLException(e.toString());
                    sqlException.initCause(e);
                    throw sqlException;
                }
                throw new SQLException("ConnectionImple.registerDatabase - " + jdbcLogger.i18NLogger.get_enlistfailed());
            } else {
                getModifier();
                if (_theModifier == null) {
                    jdbcLogger.i18NLogger.info_closingconnectionnull(_theConnection.toString());
                // no indication about connections, so assume close immediately
                /*
						 * Don't return just yet. Drop through bottom of these clauses and
	                     * close _theConnection and _recoveryConnection.
	                     *
	                     * delayClose is false at this point.
	                     *
	                     * JBTM-789.
	                     */
                } else {
                    if (((ConnectionModifier) _theModifier).supportsMultipleConnections()) {
                        /*
	                         * We can't close the connection until the transaction has
	                         * terminated, so register a Synchronization here.
	                         */
                        jdbcLogger.i18NLogger.debug_closingconnection(_theConnection.toString());
                        jtaPropertyManager.getJTAEnvironmentBean().getTransactionSynchronizationRegistry().registerInterposedSynchronization(new ConnectionSynchronization(this));
                    }
                }
            }
        } catch (RollbackException e1) {
            SQLException sqlException = new SQLException("ConnectionImple.registerDatabase - " + e1);
            sqlException.initCause(e1);
            throw sqlException;
        } catch (SystemException e2) {
            SQLException sqlException = new SQLException("ConnectionImple.registerDatabase - " + e2);
            sqlException.initCause(e2);
            throw sqlException;
        } catch (SQLException e3) {
            throw e3;
        } catch (Exception e4) {
            SQLException sqlException = new SQLException(e4.toString());
            sqlException.initCause(e4);
            throw sqlException;
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) RecoverableXAConnection(com.arjuna.ats.jta.xa.RecoverableXAConnection) XAModifier(com.arjuna.ats.jta.xa.XAModifier) RollbackException(javax.transaction.RollbackException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) SystemException(javax.transaction.SystemException) XAResource(javax.transaction.xa.XAResource) SystemException(javax.transaction.SystemException) ConnectionModifier(com.arjuna.ats.internal.jdbc.drivers.modifiers.ConnectionModifier)

Aggregations

ConnectionModifier (com.arjuna.ats.internal.jdbc.drivers.modifiers.ConnectionModifier)1 RecoverableXAConnection (com.arjuna.ats.jta.xa.RecoverableXAConnection)1 XAModifier (com.arjuna.ats.jta.xa.XAModifier)1 Connection (java.sql.Connection)1 SQLClientInfoException (java.sql.SQLClientInfoException)1 SQLException (java.sql.SQLException)1 XAConnection (javax.sql.XAConnection)1 RollbackException (javax.transaction.RollbackException)1 SystemException (javax.transaction.SystemException)1 XAResource (javax.transaction.xa.XAResource)1