Search in sources :

Example 21 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 22 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)

Example 23 with XAConnection

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

the class TransactionLogTest method testRequired2PCNoRecovery.

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

Example 24 with XAConnection

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

the class GemFireTransactionDataSource method connectionClosed.

/**
   * Implementation of call back function from ConnectionEventListener interface. This callback will
   * be invoked on connection close event.
   * 
   * @param event Connection event object
   */
public void connectionClosed(ConnectionEvent event) {
    if (isActive) {
        try {
            XAConnection conn = (XAConnection) event.getSource();
            XAResource xar = (XAResource) xaResourcesMap.get(conn);
            xaResourcesMap.remove(conn);
            Transaction txn = transManager.getTransaction();
            if (txn != null && xar != null)
                txn.delistResource(xar, XAResource.TMSUCCESS);
            provider.returnConnection(conn);
        } catch (Exception e) {
            String exception = "GemFireTransactionDataSource::connectionClosed: Exception occurred due to " + e;
            if (logger.isDebugEnabled()) {
                logger.debug(exception, e);
            }
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) Transaction(javax.transaction.Transaction) SQLException(java.sql.SQLException) XAConnection(javax.sql.XAConnection)

Example 25 with XAConnection

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

the class AbstractPoolCacheJUnitTest method testEffectOfBlockingTimeoutOnXAConnection.

/**
   * Tests if an XAresource obtained from an XAConnection which is already closed , can return null
   * or not.
   */
@Ignore("TODO: test used to eat its own exception and it fails")
@Test
public void testEffectOfBlockingTimeoutOnXAConnection() throws Exception {
    Map map = new HashMap();
    map.put("init-pool-size", "2");
    map.put("jndi-name", "TestXAPooledDataSource");
    map.put("max-pool-size", "7");
    map.put("idle-timeout-seconds", "20");
    map.put("blocking-timeout-seconds", "2");
    map.put("login-timeout-seconds", "5");
    // map.put("xa-datasource-class","org.apache.derby.jdbc.EmbeddedXADataSource");
    map.put("jdbc-driver-class", "org.apache.derby.jdbc.EmbeddedDriver");
    map.put("user-name", "mitul");
    map.put("password", "83f0069202c571faf1ae6c42b4ad46030e4e31c17409e19a");
    map.put("connection-url", "jdbc:derby:newDB;create=true");
    List props = new ArrayList();
    props.add(new ConfigProperty("databaseName", "newDB", "java.lang.String"));
    GemFireBasicDataSource gbds = (GemFireBasicDataSource) DataSourceFactory.getSimpleDataSource(map, props);
    map.put("xa-datasource-class", "org.apache.derby.jdbc.EmbeddedXADataSource");
    map.put("connection-url", "jdbc:derby:newDB;create=true");
    GemFireTransactionDataSource gtds = (GemFireTransactionDataSource) DataSourceFactory.getTranxDataSource(map, props);
    XAConnection xaconn = (XAConnection) gtds.provider.borrowConnection();
    try {
        Thread.sleep(4);
    } catch (InterruptedException e) {
        fail("interrupted");
    }
    for (int i = 0; i < 1000; ++i) {
        XAResource xar = xaconn.getXAResource();
        System.out.println("XAResource=" + xar);
        assertNotNull(xar);
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) XAConnection(javax.sql.XAConnection) Ignore(org.junit.Ignore) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

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