Search in sources :

Example 36 with UserTransaction

use of javax.transaction.UserTransaction in project geode by apache.

the class JtaNoninvolvementJUnitTest method test000Noninvolvement.

@Test
public void test000Noninvolvement() throws Exception {
    try {
        if (cache == null) {
            createCache(false);
        }
        javax.transaction.UserTransaction ut = (javax.transaction.UserTransaction) cache.getJNDIContext().lookup("java:/UserTransaction");
        {
            ut.begin();
            txRegion.put("transactionalPut", "xxx");
            assertTrue("expect cache to be in a transaction", cache.getCacheTransactionManager().exists());
            ut.commit();
        }
        assertFalse("ensure there is no transaction before testing non-involvement", cache.getCacheTransactionManager().exists());
        {
            ut.begin();
            nonTxRegion.put("nontransactionalPut", "xxx");
            assertFalse("expect cache to not be in a transaction", cache.getCacheTransactionManager().exists());
            ut.commit();
        }
    } finally {
        closeCache();
        cache = null;
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) UserTransaction(javax.transaction.UserTransaction) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 37 with UserTransaction

use of javax.transaction.UserTransaction in project geode by apache.

the class JtaNoninvolvementJUnitTest method test001NoninvolvementMultipleRegions_bug45541.

@Test
public void test001NoninvolvementMultipleRegions_bug45541() throws Exception {
    javax.transaction.UserTransaction ut = null;
    try {
        if (cache == null) {
            createCache(false);
        }
        final CountDownLatch l = new CountDownLatch(1);
        final AtomicBoolean exceptionOccurred = new AtomicBoolean(false);
        ut = (UserTransaction) cache.getJNDIContext().lookup("java:/UserTransaction");
        ut.begin();
        txRegion.put("key", "value");
        nonTxRegion.put("key", "value");
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                if (txRegion.get("key") != null) {
                    exceptionOccurred.set(true);
                }
                if (nonTxRegion.get("key") != null) {
                    exceptionOccurred.set(true);
                }
                l.countDown();
            }
        });
        t.start();
        l.await();
        assertFalse(exceptionOccurred.get());
    } finally {
        if (ut != null) {
            ut.commit();
        }
        closeCache();
        cache = null;
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UserTransaction(javax.transaction.UserTransaction) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 38 with UserTransaction

use of javax.transaction.UserTransaction in project geode by apache.

the class DataSourceJTAJUnitTest method testInsertUpdateOnSimpleAndXAdsRollback.

/**
   * The following test scenario is to test rollback. In this test scenario it is tested that if
   * transactions are rolled back then XA datasource transactions should be rolled back andSimple
   * datasource transactions should get committed
   */
@Test
public void testInsertUpdateOnSimpleAndXAdsRollback() throws Exception {
    Region currRegion = null;
    Cache cache;
    DistributedSystem ds = null;
    int tblIDFld;
    String tblNameFld;
    String tblName;
    String tableName = null;
    // boolean to_continue = true;
    final int XA_INSERTS = 3;
    final int SM_INSERTS = 1;
    // call to init method
    try {
        Properties props = new Properties();
        String path = TestUtil.getResourcePath(CacheUtils.class, "cachejta.xml");
        props.setProperty(CACHE_XML_FILE, path);
        ds = connect(props);
        tableName = CacheUtils.init(ds, "JTATest");
        System.out.println("Table name: " + tableName);
        tblName = tableName;
        if (tableName == null || tableName.equals("")) {
            // to_continue = false;
            fail(" table name not created, Aborting test...");
        }
    } catch (Exception e) {
        // to_continue = false;
        fail(" Aborting test at set up...[" + e + "]", e);
    }
    System.out.println("init for testscenario 2 Successful!");
    // test task
    cache = CacheUtils.getCache();
    tblName = tableName;
    currRegion = cache.getRegion("/root");
    tblIDFld = 1;
    tblNameFld = "test2";
    JTAUtils jtaObj = new JTAUtils(cache, currRegion);
    // to delete all rows inserted in creatTable () of CacheUtils class
    // deleteRows method of JTAUtils class is used.
    jtaObj.deleteRows(tblName);
    // initialize cache and get user transaction
    Context ctx = cache.getJNDIContext();
    UserTransaction ta = null;
    Connection xa_conn = null;
    Connection sm_conn = null;
    try {
        ta = (UserTransaction) ctx.lookup("java:/UserTransaction");
    } catch (NamingException e) {
        fail(" fail in user txn lookup ", e);
    }
    try {
        // get the SimpleDataSource before the transaction begins
        DataSource sds = (DataSource) ctx.lookup("java:/SimpleDataSource");
        // Begin the user transaction
        ta.begin();
        // Obtain XAPooledDataSource
        DataSource da = (DataSource) ctx.lookup("java:/XAPooledDataSource");
        // obtain connection from SimpleDataSource and XAPooledDataSource
        xa_conn = da.getConnection();
        sm_conn = sds.getConnection();
        Statement xa_stmt = xa_conn.createStatement();
        Statement sm_stmt = sm_conn.createStatement();
        // perform inserts and updates using both viz. Simple and XA DataSources
        // String sqlSTR = "insert into " + tblName + " values (" + tblIDFld + ","
        // + "'" + tblNameFld + "'" + ")" ;
        String sqlSTR;
        // insert XA_INSERTS rows into timestamped table
        for (int i = 0; i < XA_INSERTS; i++) {
            tblIDFld = tblIDFld + i;
            sqlSTR = "insert into " + tblName + " values (" + tblIDFld + "," + "'" + tblNameFld + "'" + ")";
            xa_stmt.executeUpdate(sqlSTR);
        }
        // insert SM_INSERTS rows into timestamped table
        for (int j = 0; j < SM_INSERTS; j++) {
            tblIDFld = tblIDFld + j + 1;
            sqlSTR = "insert into " + tblName + " values (" + tblIDFld + "," + "'" + tblNameFld + "'" + ")";
            sm_stmt.executeUpdate(sqlSTR);
        }
        // close the Simple and XA statements
        xa_stmt.close();
        sm_stmt.close();
        // close the connections
        xa_conn.close();
        sm_conn.close();
        // rollback the transaction
        ta.rollback();
        System.out.println("Rows are rolled back in the database");
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (Exception e) {
        fail(" failed ", e);
    } finally {
        if (xa_conn != null) {
            try {
                // close the connections
                xa_conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    try {
        int num_rows = jtaObj.getRows(tblName);
        System.out.println("\nNumber of rows in Table under tests are: " + num_rows);
        switch(num_rows) {
            case 0:
                System.out.println("Both Simple and XA DataSource transactions got rolled back!!");
                break;
            case SM_INSERTS:
                System.out.println("Only Simple DataSource transactions got committed!");
                break;
            case XA_INSERTS:
                System.out.println("Only XA DataSource transactions got committed!");
                break;
            default:
                System.out.println("Looks like that things are messed up...look into it");
        }
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
    System.out.println("test task for testScenario2 Succeessful");
    // destroying table
    try {
        System.out.println("Destroying table: " + tblName);
        CacheUtils.destroyTable(tblName);
        System.out.println("Closing cache...");
        System.out.println("destroyTable for testscenario 2 Successful!");
    } catch (Exception e) {
        fail(" failed during tear down of this test...", e);
    } finally {
        CacheUtils.closeCache();
        // CacheUtils.ds.disconnect();
        ds.disconnect();
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) DistributedSystem(org.apache.geode.distributed.DistributedSystem) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource) Region(org.apache.geode.cache.Region) NamingException(javax.naming.NamingException) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 39 with UserTransaction

use of javax.transaction.UserTransaction in project geode by apache.

the class DataSourceJTAJUnitTest method testMultipleXAResourceCommit.

/**
   * This is to test that if commit is issues thencommit happen for all the XA Resources
   * operations...involved in the transaction
   */
@Test
public void testMultipleXAResourceCommit() throws Exception {
    Region currRegion = null;
    Cache cache;
    DistributedSystem ds = null;
    int tblIDFld;
    String tblNameFld;
    String tblName;
    String tableName = null;
    // boolean to_continue = true;
    final int XA_INSERTS = 3;
    // call to init method
    try {
        Properties props = new Properties();
        String path = TestUtil.getResourcePath(CacheUtils.class, "cachejta.xml");
        props.setProperty(CACHE_XML_FILE, path);
        ds = connect(props);
        tableName = CacheUtils.init(ds, "JTATest");
        // System.out.println ("Table name: " + tableName);
        tblName = tableName;
        if (tableName == null || tableName.equals("")) {
            // to_continue = false;
            fail(" table name not created, Aborting test...");
        }
    } catch (Exception e) {
        // to_continue = false;
        fail(" Aborting test at set up...[" + e + "]", e);
    }
    System.out.println("init for testscenario 5 Successful!");
    // test task
    cache = CacheUtils.getCache();
    tblName = tableName;
    currRegion = cache.getRegion("/root");
    tblIDFld = 1;
    tblNameFld = "test5";
    JTAUtils jtaObj = new JTAUtils(cache, currRegion);
    // to delete all rows inserted in creatTable () of CacheUtils class
    // deleteRows method of JTAUtils class is used.
    jtaObj.deleteRows(tblName);
    // initialize cache and get user transaction
    Context ctx = cache.getJNDIContext();
    UserTransaction ta = null;
    Connection xaCon1 = null, xaCon2 = null, xaCon3 = null;
    try {
        ta = (UserTransaction) ctx.lookup("java:/UserTransaction");
    } catch (NamingException e) {
        fail(" fail in user txn lookup ", e);
    }
    try {
        // Begin the user transaction
        ta.begin();
        // Operation with first XA Resource
        DataSource da1 = (DataSource) ctx.lookup("java:/XAPooledDataSource");
        xaCon1 = da1.getConnection();
        Statement stmt1 = xaCon1.createStatement();
        String sqlSTR;
        for (int i = 0; i < XA_INSERTS; i++) {
            tblIDFld = tblIDFld + i;
            sqlSTR = "insert into " + tblName + " values (" + tblIDFld + "," + "'" + tblNameFld + "'" + ")";
            stmt1.executeUpdate(sqlSTR);
        }
        stmt1.close();
        xaCon1.close();
        // Operation with second XA Resource
        DataSource da2 = (DataSource) ctx.lookup("java:/XAPooledDataSource");
        xaCon2 = da2.getConnection();
        Statement stmt2 = xaCon2.createStatement();
        for (int i = 0; i < XA_INSERTS; i++) {
            tblIDFld = tblIDFld + i + 5;
            sqlSTR = "insert into " + tblName + " values (" + tblIDFld + "," + "'" + tblNameFld + "'" + ")";
            stmt2.executeUpdate(sqlSTR);
        }
        stmt2.close();
        xaCon2.close();
        // Operation with third XA Resource
        DataSource da3 = (DataSource) ctx.lookup("java:/XAPooledDataSource");
        xaCon3 = da3.getConnection();
        Statement stmt3 = xaCon3.createStatement();
        for (int i = 0; i < XA_INSERTS; i++) {
            tblIDFld = tblIDFld + 10;
            sqlSTR = "insert into " + tblName + " values (" + tblIDFld + "," + "'" + tblNameFld + "'" + ")";
            stmt3.executeUpdate(sqlSTR);
        }
        stmt3.close();
        xaCon3.close();
        // commit the transaction
        ta.commit();
        System.out.println("Rows are rolled back in the database");
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (Exception e) {
        fail(" failed ", e);
    } finally {
        if (xaCon1 != null || xaCon2 != null || xaCon3 != null) {
            try {
                // close the connections
                xaCon1.close();
                xaCon2.close();
                xaCon3.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    try {
        int num_rows = jtaObj.getRows(tblName);
        System.out.println("\nNumber of rows in Table under tests are: " + num_rows);
        switch(num_rows) {
            case 0:
                System.out.println("Nothing is committed to database");
                break;
            case 3 * XA_INSERTS:
                System.out.println("All inserts successfully are committed to database");
                break;
            default:
                System.out.println("Looks like that things are messed up...look into it");
        }
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
    System.out.println("test task for testScenario5 Succeessful");
    // destroying table
    try {
        System.out.println("Destroying table: " + tblName);
        CacheUtils.destroyTable(tblName);
        System.out.println("Closing cache...");
        System.out.println("destroyTable for testscenario 5 Successful!");
    } catch (Exception e) {
        fail(" failed during tear down of this test...", e);
    } finally {
        CacheUtils.closeCache();
        // CacheUtils.ds.disconnect();
        ds.disconnect();
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) DistributedSystem(org.apache.geode.distributed.DistributedSystem) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource) Region(org.apache.geode.cache.Region) NamingException(javax.naming.NamingException) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 40 with UserTransaction

use of javax.transaction.UserTransaction in project geode by apache.

the class CommitThread method run.

/*
   * Following the the run method of this thread. This method is implemented to inserts the rows in
   * the database and commit them
   *
   */
public void run() {
    // Region currRegion=null;
    Cache cache;
    int tblIDFld;
    String tblNameFld;
    // boolean to_continue = true;
    final int XA_INSERTS = 2;
    // get the cache
    // this is used to get the context for transaction later in the same method
    cache = TxnManagerMultiThreadDUnitTest.getCache();
    // get the table name from CacheUtils
    String tblName = CacheUtils.getTableName();
    tblIDFld = 1;
    tblNameFld = "thdOneCommit";
    // initialize cache and get user transaction
    Context ctx = cache.getJNDIContext();
    UserTransaction ta = null;
    Connection xa_conn = null;
    try {
        ta = (UserTransaction) ctx.lookup("java:/UserTransaction");
    // ta.setTransactionTimeout(300);
    } catch (NamingException nme) {
        nme.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        DataSource d1 = (DataSource) ctx.lookup("java:/SimpleDataSource");
        Connection con = d1.getConnection();
        con.close();
        // Begin the user transaction
        ta.begin();
        // Obtain XAPooledDataSource
        DataSource da = (DataSource) ctx.lookup("java:/XAPooledDataSource");
        // obtain connection from XAPooledDataSource
        xa_conn = da.getConnection();
        Statement xa_stmt = xa_conn.createStatement();
        String sqlSTR;
        // get the unique value for key to be inserted
        int uniqueKey = getUniqueKey();
        // insert XA_INSERTS rows into timestamped table
        for (int i = 0; i < XA_INSERTS; i++) {
            tblIDFld = tblIDFld + uniqueKey + i;
            sqlSTR = "insert into " + tblName + " values (" + tblIDFld + "," + "'" + tblNameFld + "'" + ")";
            // log.info("Thread= "+Thread.currentThread()+" ... sqlStr= "+ sqlSTR + "Before update");
            xa_stmt.executeUpdate(sqlSTR);
        // log.info("Thread= "+Thread.currentThread()+" ... sqlStr= "+ sqlSTR + "after update");
        }
        // close the Simple and XA statements
        xa_stmt.close();
        // close the connections
        xa_conn.close();
        // log.info("Thread Before Commit..."+Thread.currentThread());
        // commit the transaction
        ta.commit();
    } catch (NamingException nme) {
        nme.printStackTrace();
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (xa_conn != null) {
            try {
                // close the connections
                xa_conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    log.info(XA_INSERTS + ": Rows were inserted and committed successfully");
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) NamingException(javax.naming.NamingException) Cache(org.apache.geode.cache.Cache) DataSource(javax.sql.DataSource)

Aggregations

UserTransaction (javax.transaction.UserTransaction)224 Test (org.junit.Test)105 Context (javax.naming.Context)47 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)46 Node (javax.jcr.Node)42 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)42 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)42 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)35 DataSource (javax.sql.DataSource)30 Connection (java.sql.Connection)27 NamingException (javax.naming.NamingException)25 IOException (java.io.IOException)24 Statement (java.sql.Statement)23 Session (javax.jcr.Session)23 InitialContext (javax.naming.InitialContext)22 SQLException (java.sql.SQLException)20 SystemException (javax.transaction.SystemException)17 RollbackException (javax.transaction.RollbackException)16 Region (org.apache.geode.cache.Region)13 Lock (javax.jcr.lock.Lock)12