Search in sources :

Example 66 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 67 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 68 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)

Example 69 with XAConnection

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

the class TransactionLogTest method testRequiredRecoverable.

@Test
public void testRequiredRecoverable() throws Exception {
    XAConnection xaConn = dataSource.getXAConnection();
    try {
        txControl.required(() -> {
            txControl.getCurrentContext().registerXAResource(xaConn.getXAResource(), "foo");
            Connection conn = xaConn.getConnection();
            return conn.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
        });
    } finally {
        xaConn.close();
    }
    try (Connection conn = dataSource.getConnection()) {
        ResultSet rs = conn.createStatement().executeQuery("Select * from TEST_TABLE");
        rs.next();
        assertEquals("Hello World!", rs.getString(1));
    }
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 70 with XAConnection

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

the class TransactionLogTest method testRequiredRecoveryRequiredPrePrepare.

@Test
public void testRequiredRecoveryRequiredPrePrepare() throws Exception {
    doRecoveryRequired((good, poison) -> {
        txControl.getCurrentContext().registerXAResource(poison, null);
        txControl.getCurrentContext().registerXAResource(good, "foo");
    }, TransactionStatus.ROLLED_BACK);
    boolean success = false;
    XAConnection conn = dataSource.getXAConnection();
    for (int i = 0; i < 5; i++) {
        if (conn.getXAResource().recover(XAResource.TMSTARTRSCAN).length == 0) {
            success = true;
            break;
        } else {
            // Wait for recovery to happen!
            Thread.sleep(500);
        }
    }
    assertTrue("No recovery in time", success);
}
Also used : XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Aggregations

XAConnection (javax.sql.XAConnection)115 Connection (java.sql.Connection)78 XAResource (javax.transaction.xa.XAResource)56 XADataSource (javax.sql.XADataSource)52 Xid (javax.transaction.xa.Xid)44 Statement (java.sql.Statement)34 SQLException (java.sql.SQLException)30 PreparedStatement (java.sql.PreparedStatement)23 PooledConnection (javax.sql.PooledConnection)23 ResultSet (java.sql.ResultSet)22 CallableStatement (java.sql.CallableStatement)18 Test (org.junit.Test)18 XAException (javax.transaction.xa.XAException)16 Transaction (javax.transaction.Transaction)8 DataSource (javax.sql.DataSource)7 XidImpl (org.firebirdsql.jca.TestXABase.XidImpl)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 NotSupportedException (javax.transaction.NotSupportedException)5 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4 J2EEDataSource (org.apache.derbyTesting.junit.J2EEDataSource)4