Search in sources :

Example 6 with JTAUtils

use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.

the class CacheJUnitTest method testScenario15.

/**
   * Test of testScenario15 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
   * Tests performing DDL operations by looking up a XAPooledDataSource and without transaction.
   */
@Test
public void testScenario15() throws Exception {
    this.tblIDFld = 15;
    this.tblNameFld = "test15";
    String tbl = "";
    boolean row_num = true;
    int ddl_return = 1;
    JTAUtils jtaObj = new JTAUtils(cache, currRegion);
    // delete the rows inserted from CacheUtils createTable, otherwise conflict
    // in PK's. Basically not needed for this test
    jtaObj.deleteRows(this.tblName);
    Context ctx = cache.getJNDIContext();
    // UserTransaction ta = null;
    Connection conn = null;
    Statement stmt = null;
    try {
        DataSource da = (DataSource) ctx.lookup("java:/XAPooledDataSource");
        conn = da.getConnection();
        stmt = conn.createStatement();
        // Do some DDL stuff
        String time = new Long(System.currentTimeMillis()).toString();
        tbl = "my_table" + time;
        // String sql = "create table " + tbl +
        // " (my_id number primary key, my_name varchar2(50))";
        String sql = "create table " + tbl + " (my_id integer NOT NULL, my_name varchar(50), CONSTRAINT my_keyx PRIMARY KEY(my_id))";
        ddl_return = stmt.executeUpdate(sql);
        // check whether the table was created properly
        sql = "select * from " + tbl;
        ResultSet rs = null;
        rs = stmt.executeQuery(sql);
        row_num = rs.next();
        rs.close();
        if (ddl_return == 0 && !row_num) {
            sql = "drop table " + tbl;
            try {
                stmt = conn.createStatement();
                stmt.executeUpdate(sql);
            } catch (SQLException e) {
                fail(" failed to drop, " + e.getMessage());
            }
        } else
            fail("unable to create table");
        /** Code meant for Oracle DB **/
        /*
       * tbl = tbl.toUpperCase(); sql = "select * from tab where tname like '%tbl%'"; ResultSet rs =
       * null; rs = stmt.executeQuery(sql); rs.close(); sql = "drop table "+tbl; stmt =
       * conn.createStatement(); stmt.execute(sql); System.out.println (this.space + "ok");
       */
        stmt.close();
        conn.close();
    } catch (NamingException e) {
        fail("failed, " + e.getMessage());
    } catch (SQLException e) {
        fail("failed, " + e.getMessage());
    } catch (Exception e) {
        fail("failed, " + e.getMessage());
    } finally {
        if (conn != null)
            conn.close();
    }
}
Also used : Context(javax.naming.Context) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JTAUtils(org.apache.geode.internal.jta.JTAUtils) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheExistsException(org.apache.geode.cache.CacheExistsException) CacheException(org.apache.geode.cache.CacheException) DataSource(javax.sql.DataSource) ResultSet(java.sql.ResultSet) NamingException(javax.naming.NamingException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 7 with JTAUtils

use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.

the class CacheJUnitTest method testScenario14.

/**
   * Test of testScenario14 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
   * Tests a local Cache Loader with XADataSource lookup to get the connection. The Connection
   * should not participate in the transaction and commit/rollback should take affect accordingly
   * along with cache.
   */
@Test
public void testScenario14() throws Exception {
    final String TABLEID = "2";
    // final String TABLEFLD = "name2";
    JTAUtils jtaObj = new JTAUtils(cache, currRegion);
    Context ctx = cache.getJNDIContext();
    UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
    utx.begin();
    AttributesFactory fac = new AttributesFactory(currRegion.getAttributes());
    fac.setCacheLoader(new XACacheLoaderTxn(this.tblName));
    Region re = currRegion.createSubregion("employee", fac.create());
    // TABLEID correspondes to
    String retVal = (String) re.get(TABLEID);
    // "name1".
    if (!retVal.equals("newname"))
        fail("Uncommitted value 'newname' not read by cacheloader name = " + retVal);
    utx.rollback();
    DataSource ds = (DataSource) ctx.lookup("java:/XAPooledDataSource");
    Connection conn = ds.getConnection();
    Statement stm = conn.createStatement();
    String str = "select name from " + tblName + "  where id= (2)";
    ResultSet rs = stm.executeQuery(str);
    rs.next();
    String str1 = rs.getString(1);
    if (!str1.equals("name2"))
        fail("Rollback not occurred on XAConnection got in a cache loader");
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) AttributesFactory(org.apache.geode.cache.AttributesFactory) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Region(org.apache.geode.cache.Region) JTAUtils(org.apache.geode.internal.jta.JTAUtils) DataSource(javax.sql.DataSource) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 8 with JTAUtils

use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.

the class CacheJUnitTest method testScenario10.

/**
   * Test of testScenario10 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
   * Test a user transaction with cache lookup and with XAPooledDataSource for multiple JDBC
   * Connections. This should not be allowed for Facets datasource. For other relational DB the
   * behaviour will be DB specific. For Oracle DB, n connections in a tranxn can be used provided ,
   * n-1 connections are closed before opening nth connection.
   */
@Test
public void testScenario10() throws Exception {
    this.tblIDFld = 10;
    this.tblNameFld = "test10";
    int rows_inserted = 0;
    int ifAnyRows = 0;
    int field1 = this.tblIDFld, field2 = 0;
    JTAUtils jtaObj = new JTAUtils(cache, currRegion);
    jtaObj.deleteRows(this.tblName);
    Context ctx = cache.getJNDIContext();
    UserTransaction ta = (UserTransaction) ctx.lookup("java:/UserTransaction");
    Connection conn1 = null;
    Connection conn2 = null;
    try {
        ta.begin();
        DataSource da = (DataSource) ctx.lookup("java:/XAPooledDataSource");
        // the first Connection
        conn1 = da.getConnection();
        Statement stmt = conn1.createStatement();
        String sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
        stmt.executeUpdate(sqlSTR);
        rows_inserted += 1;
        conn1.close();
        // the second Connection
        conn2 = da.getConnection();
        stmt = conn2.createStatement();
        this.tblIDFld += 1;
        field2 = this.tblIDFld;
        sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
        stmt.executeUpdate(sqlSTR);
        rows_inserted += 1;
        stmt.close();
        conn2.close();
        ta.commit();
        // if we reach here check for proper entries in db
        ifAnyRows = jtaObj.getRows(this.tblName);
        if (ifAnyRows == rows_inserted) {
            boolean matched1 = jtaObj.checkTableAgainstData(this.tblName, (field1 + ""));
            boolean matched2 = jtaObj.checkTableAgainstData(this.tblName, (field2 + ""));
            if (matched1)
                System.out.print("(PK " + field1 + "found ");
            else
                System.out.print("(PK " + field1 + "not found ");
            if (matched2)
                System.out.print("PK " + field2 + "found)");
            else
                System.out.print("PK " + field2 + "not found)");
            if (matched1 & matched2) {
                System.out.println("ok");
            } else {
                fail(" inserted data not found in DB !... failed");
            }
        } else {
            fail(" test interrupted, rows found=" + ifAnyRows + ", rows inserted=" + rows_inserted);
        }
    } catch (NamingException e) {
        ta.rollback();
    } catch (SQLException e) {
        ta.rollback();
    } catch (Exception e) {
        ta.rollback();
    } finally {
        if (conn1 != null)
            try {
                conn1.close();
            } catch (SQLException e) {
            }
        if (conn2 != null)
            try {
                conn2.close();
            } catch (SQLException e) {
            }
    }
}
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) JTAUtils(org.apache.geode.internal.jta.JTAUtils) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheExistsException(org.apache.geode.cache.CacheExistsException) CacheException(org.apache.geode.cache.CacheException) DataSource(javax.sql.DataSource) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 9 with JTAUtils

use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.

the class CacheJUnitTest method testScenario7.

/**
   * Test of testScenario7 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
   * Tests a user transaction with cache lookup and with XAPooledDataSource lookup. It does get and
   * put operations on the cache within the transaction block and checks whether these operations
   * are committed once the transaction is committed.
   */
@Test
public void testScenario7() throws Exception {
    this.tblIDFld = 7;
    this.tblNameFld = "test7";
    boolean rollback_chances = true;
    final String DEFAULT_RGN = "root";
    JTAUtils jtaObj = new JTAUtils(cache, currRegion);
    jtaObj.deleteRows(this.tblName);
    Context ctx = cache.getJNDIContext();
    UserTransaction ta = (UserTransaction) ctx.lookup("java:/UserTransaction");
    Connection conn = null;
    try {
        ta.begin();
        String current_region = jtaObj.currRegion.getName();
        assertEquals("default region is not root", DEFAULT_RGN, current_region);
        jtaObj.getRegionFromCache("region1");
        // now current region should point to region1, as done from within
        // getRegionFromCache method...
        String current_fullpath = jtaObj.currRegion.getFullPath();
        assertEquals("failed retrieving the current region fullpath", "/" + DEFAULT_RGN + "/region1", current_fullpath);
        jtaObj.put("key1", "value1");
        // try to get value1 from key1 in the same region
        String str = jtaObj.get("key1");
        String tok = jtaObj.parseGetValue(str);
        assertEquals("get value mismatch with put", "\"value1\"", tok);
        current_fullpath = jtaObj.currRegion.getFullPath();
        assertEquals("failed retrieving current region fullpath", "/" + DEFAULT_RGN + "/region1", current_fullpath);
        DataSource da = (DataSource) ctx.lookup("java:/XAPooledDataSource");
        conn = da.getConnection();
        ta.commit();
        conn.close();
        rollback_chances = false;
        current_fullpath = jtaObj.currRegion.getFullPath();
        assertEquals("failed retrieving current region fullpath after txn commit, fullpath is: " + current_region, "/" + DEFAULT_RGN + "/region1", current_fullpath);
        str = jtaObj.get("key1");
        tok = jtaObj.parseGetValue(str);
        assertEquals("cache value found is: " + tok, "\"value1\"", tok);
    } catch (CacheExistsException e) {
        if (rollback_chances)
            ta.rollback();
        fail(" failed due to: " + e.getMessage());
    } catch (NamingException e) {
        ta.rollback();
        fail(" failed due to: " + e.getMessage());
    } catch (SQLException e) {
        ta.rollback();
        fail(" failed due to: " + e.getMessage());
    } catch (Exception e) {
        if (rollback_chances)
            ta.rollback();
        fail(" failed due to: " + e.getMessage());
    } finally {
        if (conn != null)
            try {
                conn.close();
            } catch (Exception e) {
            }
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CacheExistsException(org.apache.geode.cache.CacheExistsException) NamingException(javax.naming.NamingException) JTAUtils(org.apache.geode.internal.jta.JTAUtils) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheExistsException(org.apache.geode.cache.CacheExistsException) CacheException(org.apache.geode.cache.CacheException) DataSource(javax.sql.DataSource) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 10 with JTAUtils

use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.

the class TxnManagerMultiThreadDUnitTest method delRows.

public static void delRows() {
    Region currRegion = null;
    Cache cache;
    // get the cache
    // this is used to get the context for passing to the constructor of
    // JTAUtils
    cache = TxnManagerMultiThreadDUnitTest.getCache();
    currRegion = cache.getRegion("/root");
    JTAUtils jtaObj = new JTAUtils(cache, currRegion);
    // deleteRows method of JTAUtils class is used.
    try {
        // get the table name from CacheUtils
        String tblName_delRows = CacheUtils.getTableName();
        /* int rowsDeleted = */
        jtaObj.deleteRows(tblName_delRows);
    } catch (Exception e) {
        fail("Error: while deleting rows from database using JTAUtils", e);
    }
}
Also used : Region(org.apache.geode.cache.Region) JTAUtils(org.apache.geode.internal.jta.JTAUtils) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) IOException(java.io.IOException) Cache(org.apache.geode.cache.Cache)

Aggregations

JTAUtils (org.apache.geode.internal.jta.JTAUtils)15 SQLException (java.sql.SQLException)14 Connection (java.sql.Connection)13 Context (javax.naming.Context)13 NamingException (javax.naming.NamingException)13 DataSource (javax.sql.DataSource)13 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)13 Test (org.junit.Test)13 Statement (java.sql.Statement)12 UserTransaction (javax.transaction.UserTransaction)11 CacheException (org.apache.geode.cache.CacheException)11 CacheExistsException (org.apache.geode.cache.CacheExistsException)11 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)11 ResultSet (java.sql.ResultSet)3 Region (org.apache.geode.cache.Region)3 IOException (java.io.IOException)2 Cache (org.apache.geode.cache.Cache)2 AttributesFactory (org.apache.geode.cache.AttributesFactory)1