Search in sources :

Example 16 with XAConnection

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

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

the class XATxContextBindingEntityManagerTest method testActiveTransactionStraightXAConnection.

@Test
public void testActiveTransactionStraightXAConnection() throws SQLException {
    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();
    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 18 with XAConnection

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

the class XATxContextBindingEntityManagerTest method testActiveTransactionWrappedXAConnection.

@Test
public void testActiveTransactionWrappedXAConnection() throws SQLException {
    XAConnection con = Mockito.mock(XAConnection.class);
    Connection raw = Mockito.mock(Connection.class);
    Mockito.when(con.getXAResource()).thenReturn(xaResource);
    Mockito.when(con.getConnection()).thenReturn(raw);
    XAConnectionWrapper value = new XAConnectionWrapper(con);
    Mockito.when(rawEm.unwrap(Connection.class)).thenReturn(value);
    setupActiveTransaction();
    em.isOpen();
    em.isOpen();
    Mockito.verify(rawEm, times(2)).isOpen();
    Mockito.verify(rawEm).joinTransaction();
    checkPostCompletion(null);
}
Also used : XAConnectionWrapper(org.apache.aries.tx.control.jdbc.xa.connection.impl.XAConnectionWrapper) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 19 with XAConnection

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

the class XATxContextBindingEntityManagerTest method testActiveTransactionUnwrappableXAConnectionWrapper.

@Test
public void testActiveTransactionUnwrappableXAConnectionWrapper() throws SQLException {
    XAConnection xaCon = Mockito.mock(XAConnection.class);
    Mockito.when(xaCon.getXAResource()).thenReturn(xaResource);
    Connection con = Mockito.mock(Connection.class);
    XAConnectionWrapper toReturn = new XAConnectionWrapper(xaCon);
    Mockito.when(con.unwrap(XAConnectionWrapper.class)).thenReturn(toReturn);
    Mockito.when(con.isWrapperFor(XAConnectionWrapper.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 : XAConnectionWrapper(org.apache.aries.tx.control.jdbc.xa.connection.impl.XAConnectionWrapper) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 20 with XAConnection

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

the class TransactionLogTest method doRecoveryRequired.

public void doRecoveryRequired(BiConsumer<XAResource, XAResource> ordering, TransactionStatus expectedFinalState) throws Exception {
    //Register the recoverable resource
    ArgumentCaptor<ServiceListener> captor = ArgumentCaptor.forClass(ServiceListener.class);
    Mockito.verify(ctx).addServiceListener(captor.capture(), Mockito.anyString());
    Mockito.when(ctx.getService(serviceRef)).thenReturn(new TestRecoverableResource("foo", dataSource));
    captor.getValue().serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
    XAConnection xaConn = dataSource.getXAConnection();
    AtomicReference<TransactionStatus> ref = new AtomicReference<TransactionStatus>();
    try {
        txControl.required(() -> {
            txControl.getCurrentContext().postCompletion(ref::set);
            Connection conn = xaConn.getConnection();
            // conn.setAutoCommit(false);
            XAResource dsResource = xaConn.getXAResource();
            XAResource poison = Mockito.mock(XAResource.class);
            Mockito.when(poison.prepare(Mockito.any())).thenAnswer(i -> {
                conn.createStatement().execute("shutdown immediately");
                Thread.sleep(1000);
                return XA_OK;
            });
            ordering.accept(dsResource, poison);
            return conn.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
        });
    } catch (TransactionException te) {
        assertEquals(expectedFinalState, ref.get());
        assertEquals(expectedFinalState == ROLLED_BACK, te instanceof TransactionRolledBackException);
    } finally {
        try {
            xaConn.close();
        } catch (SQLException sqle) {
        }
    }
    setupServerAndDataSource();
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) SQLException(java.sql.SQLException) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) AtomicReference(java.util.concurrent.atomic.AtomicReference) XAResource(javax.transaction.xa.XAResource) RecoverableXAResource(org.osgi.service.transaction.control.recovery.RecoverableXAResource) TransactionException(org.osgi.service.transaction.control.TransactionException) ServiceEvent(org.osgi.framework.ServiceEvent) TransactionRolledBackException(org.osgi.service.transaction.control.TransactionRolledBackException) 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