Search in sources :

Example 6 with GemFireTransactionDataSource

use of org.apache.geode.internal.datasource.GemFireTransactionDataSource in project geode by apache.

the class TransactionTimeOutJUnitTest method test9RollbackAfterTimeOut.

@Test
public void test9RollbackAfterTimeOut() throws Exception {
    Context ctx = cache.getJNDIContext();
    DataSource ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource");
    Connection conn2 = ds2.getConnection();
    GemFireTransactionDataSource ds = (GemFireTransactionDataSource) ctx.lookup("java:/XAPooledDataSource");
    UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
    utx.begin();
    Connection conn = ds.getConnection();
    String sql = "create table newTable3 (id integer)";
    Statement sm = conn.createStatement();
    sm.execute(sql);
    utx.setTransactionTimeout(30);
    sql = "insert into newTable3  values (1)";
    sm.execute(sql);
    sql = "select * from newTable3 where id = 1";
    ResultSet rs = sm.executeQuery(sql);
    if (!rs.next()) {
        fail("Transaction not committed");
    }
    sql = "drop table newTable3";
    sm.execute(sql);
    sm.close();
    conn.close();
    conn2.close();
    utx.setTransactionTimeout(1);
    Thread.sleep(3000);
    try {
        utx.rollback();
        fail("exception did not occur on rollback although transaction timed out");
    } catch (Exception expected) {
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) GemFireTransactionDataSource(org.apache.geode.internal.datasource.GemFireTransactionDataSource) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IOException(java.io.IOException) DataSource(javax.sql.DataSource) GemFireTransactionDataSource(org.apache.geode.internal.datasource.GemFireTransactionDataSource) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 7 with GemFireTransactionDataSource

use of org.apache.geode.internal.datasource.GemFireTransactionDataSource in project geode by apache.

the class GlobalTransactionJUnitTest method testEnlistResource.

@Test
public void testEnlistResource() throws Exception {
    try {
        boolean exceptionoccurred = false;
        utx.begin();
        try {
            Context ctx = cache.getJNDIContext();
            GemFireTransactionDataSource ds = (GemFireTransactionDataSource) ctx.lookup("java:/XAPooledDataSource");
            ds.getConnection();
        } catch (SQLException e) {
            exceptionoccurred = true;
        }
        if (exceptionoccurred)
            fail("SQLException occurred while trying to enlist resource");
        utx.rollback();
    } catch (Exception e) {
        try {
            utx.rollback();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        fail("exception in testEnlistResource due to " + e);
        e.printStackTrace();
    }
}
Also used : Context(javax.naming.Context) GemFireTransactionDataSource(org.apache.geode.internal.datasource.GemFireTransactionDataSource) SQLException(java.sql.SQLException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 8 with GemFireTransactionDataSource

use of org.apache.geode.internal.datasource.GemFireTransactionDataSource in project geode by apache.

the class GlobalTransactionJUnitTest method testEnlistResourceAfterRollBack.

@Test
public void testEnlistResourceAfterRollBack() throws Exception {
    try {
        boolean exceptionoccurred = false;
        utx.begin();
        utx.setRollbackOnly();
        Context ctx = cache.getJNDIContext();
        try {
            GemFireTransactionDataSource ds = (GemFireTransactionDataSource) ctx.lookup("java:/XAPooledDataSource");
            ds.getConnection();
        } catch (SQLException e) {
            exceptionoccurred = true;
        }
        if (!exceptionoccurred)
            fail("SQLException not occurred although the transaction was marked for rollback");
        utx.rollback();
    } catch (Exception e) {
        try {
            utx.rollback();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        fail("exception in testSetRollbackonly due to " + e);
        e.printStackTrace();
    }
}
Also used : Context(javax.naming.Context) GemFireTransactionDataSource(org.apache.geode.internal.datasource.GemFireTransactionDataSource) SQLException(java.sql.SQLException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

Context (javax.naming.Context)8 GemFireTransactionDataSource (org.apache.geode.internal.datasource.GemFireTransactionDataSource)8 Connection (java.sql.Connection)6 ResultSet (java.sql.ResultSet)6 Statement (java.sql.Statement)6 DataSource (javax.sql.DataSource)6 UserTransaction (javax.transaction.UserTransaction)6 IOException (java.io.IOException)5 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)5 Test (org.junit.Test)5 SQLException (java.sql.SQLException)2 RollbackException (javax.transaction.RollbackException)2