Search in sources :

Example 6 with XAConnection

use of javax.sql.XAConnection in project aries by apache.

the class XATxContextBindingEntityManagerTest method testActiveTransactionWithPreviousCommonTxControl.

@Test
public void testActiveTransactionWithPreviousCommonTxControl() throws SQLException {
    TransactionControl previous = Mockito.mock(TransactionControl.class);
    Connection con = Mockito.mock(Connection.class, withSettings().extraInterfaces(XAConnection.class));
    Mockito.when(((XAConnection) con).getXAResource()).thenReturn(xaResource);
    Mockito.when(rawEm.unwrap(Connection.class)).thenReturn(con);
    setupActiveTransaction();
    commonTxControl.set(previous);
    em.isOpen();
    em.isOpen();
    Mockito.verify(rawEm, times(2)).isOpen();
    Mockito.verify(rawEm).joinTransaction();
    checkPostCompletion(previous);
}
Also used : TransactionControl(org.osgi.service.transaction.control.TransactionControl) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 7 with XAConnection

use of javax.sql.XAConnection in project aries by apache.

the class XATxContextBindingEntityManagerTest method testActiveTransactionUnwrappableXAConnection.

@Test
public void testActiveTransactionUnwrappableXAConnection() throws SQLException {
    XAConnection xaCon = Mockito.mock(XAConnection.class);
    Mockito.when(xaCon.getXAResource()).thenReturn(xaResource);
    Connection con = Mockito.mock(Connection.class);
    Mockito.when(con.unwrap(XAConnection.class)).thenReturn(xaCon);
    Mockito.when(con.isWrapperFor(XAConnection.class)).thenReturn(true);
    Mockito.when(rawEm.unwrap(Connection.class)).thenReturn(con);
    setupActiveTransaction();
    em.isOpen();
    em.isOpen();
    Mockito.verify(rawEm, times(2)).isOpen();
    Mockito.verify(rawEm).joinTransaction();
    checkPostCompletion(null);
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 8 with XAConnection

use of javax.sql.XAConnection in project geode by apache.

the class GemFireTransactionDataSource method getConnection.

/**
   * Implementation of datasource function. This method is used to get the connection from the pool.
   * Default user name and password will be used.
   * 
   * @throws SQLException
   * @return ???
   */
@Override
public Connection getConnection() throws SQLException {
    if (!isActive) {
        throw new SQLException(LocalizedStrings.GemFireTransactionDataSource_GEMFIRETRANSACTIONDATASOURCEGETCONNECTIONNO_VALID_CONNECTION_AVAILABLE.toLocalizedString());
    }
    XAConnection xaConn = null;
    try {
        xaConn = (XAConnection) provider.borrowConnection();
        Connection conn = getSQLConnection(xaConn);
        registerTranxConnection(xaConn);
        return conn;
    } catch (Exception ex) {
        SQLException se = new SQLException(ex.getMessage());
        se.initCause(ex);
        throw se;
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) XAConnection(javax.sql.XAConnection)

Example 9 with XAConnection

use of javax.sql.XAConnection 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 10 with XAConnection

use of javax.sql.XAConnection in project druid by alibaba.

the class MySqlUtils method createXAConnection.

public static XAConnection createXAConnection(Driver driver, Connection physicalConn) throws SQLException {
    final int major = driver.getMajorVersion();
    if (major == 5) {
        if (utilClass == null && !utilClassError) {
            try {
                utilClass = Class.forName("com.mysql.jdbc.Util");
                Method method = utilClass.getMethod("isJdbc4");
                utilClass_isJdbc4 = (Boolean) method.invoke(null);
                connectionClass = Class.forName("com.mysql.jdbc.Connection");
                getPinGlobalTxToPhysicalConnectionMethod = connectionClass.getMethod("getPinGlobalTxToPhysicalConnection");
                suspendableXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.SuspendableXAConnection");
                suspendableXAConnectionConstructor = suspendableXAConnectionClass.getConstructor(connectionClass);
                JDBC4SuspendableXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.JDBC4SuspendableXAConnection");
                JDBC4SuspendableXAConnectionConstructor = JDBC4SuspendableXAConnectionClass.getConstructor(connectionClass);
                MysqlXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.MysqlXAConnection");
                MysqlXAConnectionConstructor = MysqlXAConnectionClass.getConstructor(connectionClass, boolean.class);
            } catch (Exception ex) {
                ex.printStackTrace();
                utilClassError = true;
            }
        }
        try {
            boolean pinGlobTx = (Boolean) getPinGlobalTxToPhysicalConnectionMethod.invoke(physicalConn);
            if (pinGlobTx) {
                if (!utilClass_isJdbc4) {
                    return (XAConnection) suspendableXAConnectionConstructor.newInstance(physicalConn);
                }
                return (XAConnection) JDBC4SuspendableXAConnectionConstructor.newInstance(physicalConn);
            }
            return (XAConnection) MysqlXAConnectionConstructor.newInstance(physicalConn, Boolean.FALSE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    throw new SQLFeatureNotSupportedException();
}
Also used : SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) Method(java.lang.reflect.Method) SQLException(java.sql.SQLException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XAConnection(javax.sql.XAConnection)

Aggregations

XAConnection (javax.sql.XAConnection)25 Connection (java.sql.Connection)19 Test (org.junit.Test)14 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 XAResource (javax.transaction.xa.XAResource)4 PreparedStatement (java.sql.PreparedStatement)3 SystemException (javax.transaction.SystemException)3 Transaction (javax.transaction.Transaction)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Properties (java.util.Properties)2 RollbackException (javax.transaction.RollbackException)2 XAConnectionWrapper (org.apache.aries.tx.control.jdbc.xa.connection.impl.XAConnectionWrapper)2 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)1 IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1