Search in sources :

Example 41 with Cache

use of org.apache.geode.cache.Cache 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 42 with Cache

use of org.apache.geode.cache.Cache 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 43 with Cache

use of org.apache.geode.cache.Cache 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)

Example 44 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class TypedJsonJUnitTest method testPDXObject.

@Test
public void testPDXObject() {
    final Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    DistributedSystem.connect(props);
    Cache cache = new CacheFactory().create();
    PdxInstanceFactory pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
    Portfolio p = new Portfolio(2);
    pf.writeInt("ID", 111);
    pf.writeString("status", "active");
    pf.writeString("secId", "IBM");
    pf.writeObject("portfolio", p);
    PdxInstance pi = pf.create();
    TypedJson tJsonObj = new TypedJson(RESULT, pi);
    System.out.println(tJsonObj);
    cache.close();
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) PdxInstance(org.apache.geode.pdx.PdxInstance) Portfolio(org.apache.geode.cache.query.data.Portfolio) TypedJson(org.apache.geode.management.internal.cli.json.TypedJson) CacheFactory(org.apache.geode.cache.CacheFactory) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 45 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class TypedJsonJUnitTest method testNestedPDXObject.

@Test
public void testNestedPDXObject() {
    final Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    DistributedSystem.connect(props);
    Cache cache = new CacheFactory().create();
    PdxInstanceFactory pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
    pf.writeInt("ID", 111);
    pf.writeString("status", "active");
    pf.writeString("secId", "IBM");
    PdxInstance pi = pf.create();
    PDXContainer cont = new PDXContainer(1);
    cont.setPi(pi);
    TypedJson tJsonObj = new TypedJson(RESULT, cont);
    System.out.println(tJsonObj);
    cache.close();
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) PdxInstance(org.apache.geode.pdx.PdxInstance) TypedJson(org.apache.geode.management.internal.cli.json.TypedJson) CacheFactory(org.apache.geode.cache.CacheFactory) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

Cache (org.apache.geode.cache.Cache)1044 Region (org.apache.geode.cache.Region)478 Test (org.junit.Test)476 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)292 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)277 VM (org.apache.geode.test.dunit.VM)264 Host (org.apache.geode.test.dunit.Host)230 AttributesFactory (org.apache.geode.cache.AttributesFactory)229 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)177 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)176 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)164 LocalRegion (org.apache.geode.internal.cache.LocalRegion)153 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)123 ClientCache (org.apache.geode.cache.client.ClientCache)117 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)112 Properties (java.util.Properties)101 CacheException (org.apache.geode.cache.CacheException)101 RegionAttributes (org.apache.geode.cache.RegionAttributes)99 QueryService (org.apache.geode.cache.query.QueryService)95 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)93