use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.
the class TxnManagerMultiThreadDUnitTest method getNumberOfRows.
/*
* calldestroyTable method is written to destroyTable, close cache and disconnect from
* DistributedSystem. This method calls the destroyTable method of this class.
*/
/*
*
* public static void calldestroyTable (String tableName){
*
* //the sleep below is given to give the time to threads to start and perform before destroyTable
* is called. try { Thread.sleep(20*1000); } catch (InterruptedException ie) {
* ie.printStackTrace(); } try { String tblName = tableName; getLogWriter().fine
* ("Destroying table: " + tblName); TxnManagerMultiThreadDUnitTest.destroyTable(tblName);
* getLogWriter().fine ("Closing cache..."); getLogWriter().fine("destroyTable is Successful!"); }
* catch (Exception e) { fail (" failed during tear down of this test..." + e); } finally {
* closeCache(); ds.disconnect(); } }//end of destroyTable
*/
public static void getNumberOfRows() {
// before getNumberOfRows is called.
try {
Thread.sleep(7 * 1000);
} catch (InterruptedException ie) {
fail("interrupted", ie);
}
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();
// get the table name from CacheUtils
String tblName = CacheUtils.getTableName();
currRegion = cache.getRegion("/root");
JTAUtils jtaObj = new JTAUtils(cache, currRegion);
// get how many rows actually got committed
try {
int rows = jtaObj.getRows(tblName);
LogWriterUtils.getLogWriter().fine("Number of rows committed current test method are: " + rows);
} catch (Exception e) {
LogWriterUtils.getLogWriter().warning("Error: while getting rows from database using JTAUtils", e);
fail("Error: while getting rows from database using JTAUtils", e);
}
}
use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.
the class CacheJUnitTest method testScenario2.
/**
* Test of testScenario2 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
* Tests a simple User Transaction with Cache lookup but closing the Connection object before
* committing the Transaction.
*/
@Test
public void testScenario2() throws Exception {
this.tblIDFld = 2;
this.tblNameFld = "test2";
boolean rollback_chances = true;
JTAUtils jtaObj = new JTAUtils(cache, currRegion);
jtaObj.deleteRows(this.tblName);
Context ctx = cache.getJNDIContext();
System.out.print(" looking up UserTransaction... ");
UserTransaction ta = (UserTransaction) ctx.lookup("java:/UserTransaction");
Connection conn = null;
try {
ta.begin();
DataSource da = (DataSource) ctx.lookup("java:/XAPooledDataSource");
conn = da.getConnection();
Statement stmt = conn.createStatement();
String sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
stmt.executeUpdate(sqlSTR);
stmt.close();
conn.close();
ta.commit();
rollback_chances = false;
int ifAnyRows = jtaObj.getRows(this.tblName);
if (ifAnyRows == 0) {
// no rows are there !!! failure :)
fail(" no rows retrieved even after txn commit after conn close.");
}
} catch (NamingException e) {
if (rollback_chances)
ta.rollback();
fail(" failed " + e.getMessage());
} catch (SQLException e) {
if (rollback_chances)
ta.rollback();
fail(" failed " + e.getMessage());
} catch (Exception e) {
ta.rollback();
fail(" failed " + e.getMessage());
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
}
use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.
the class CacheJUnitTest method testScenario5.
/**
* Test of testScenario5 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
* Tests whether a user transaction with cache lookup and with XAPooledDataSOurce supports Cache
* put and get operations accordingly along with JTA behavior. Put and get are done within the
* transaction block and also db updates are done. We try to rollback the transaction by violating
* primary key constraint in db table, nad then we check whether rollback was proper in db and
* also in Cache, which should also rollback.
*/
@Test
public void testScenario5() throws Exception {
this.tblIDFld = 5;
this.tblNameFld = "test5";
boolean rollback_chances = false;
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 {
String current_region = jtaObj.currRegion.getName();
assertEquals("default region is not root", DEFAULT_RGN, current_region);
// trying to create a region 'region1' under /root, if it doesn't exist.
// And point to the new 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 retirieving current fullpath", "/" + DEFAULT_RGN + "/region1", current_fullpath);
jtaObj.put("key1", "test");
ta.begin();
jtaObj.put("key1", "value1");
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 fullpath, current fullpath: " + current_fullpath, "/" + DEFAULT_RGN + "/region1", current_fullpath);
DataSource da = (DataSource) ctx.lookup("java:/XAPooledDataSource");
conn = da.getConnection();
Statement stmt = conn.createStatement();
String sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
stmt.executeUpdate(sqlSTR);
rollback_chances = true;
// capable of throwing SQLException during insert operation
sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
stmt.executeUpdate(sqlSTR);
stmt.close();
ta.commit();
conn.close();
} catch (CacheExistsException e) {
ta.rollback();
fail("test failed " + e.getMessage());
} catch (NamingException e) {
ta.rollback();
fail("test failed " + e.getMessage());
} catch (SQLException e) {
// exception thrown during inserts are handeled as below by rollback
if (rollback_chances) {
try {
ta.rollback();
} catch (Exception ex) {
fail("failed: " + ex.getMessage());
}
// intended error is checked w.r.t database first
int ifAnyRows = 0;
try {
ifAnyRows = jtaObj.getRows(this.tblName);
} catch (Exception ex) {
fail(" failed: " + ex.getMessage());
}
assertEquals("rows found after rollback is: " + ifAnyRows, 0, ifAnyRows);
/*
* if (ifAnyRows != 0) { fail (" DB FAILURE"); }
*/
// intended error is checked w.r.t. cache second
String current_fullpath = jtaObj.currRegion.getFullPath();
assertEquals("failed retrieving current fullpath after rollback, fullpath is: " + current_fullpath, "/" + DEFAULT_RGN + "/region1", current_fullpath);
// after jdbc rollback, cache value in region1 for key1 must vanish...
String str1 = null;
try {
str1 = jtaObj.get("key1");
} catch (CacheException ex) {
fail("failed getting value for 'key1': " + ex.getMessage());
}
String tok1 = jtaObj.parseGetValue(str1);
assertEquals("value found in cache: " + tok1 + ", after rollback", "\"test\"", tok1);
} else {
fail(" failed: " + e.getMessage());
ta.rollback();
}
} catch (Exception e) {
ta.rollback();
fail(" failed: " + e.getMessage());
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException ex) {
fail("Exception: " + ex.getMessage());
}
}
}
use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.
the class CacheJUnitTest method testScenario1.
/**
* Test of testScenario1 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
* Tests a simple User Transaction with Cache lookup.
*/
@Test
public void testScenario1() throws Exception {
this.tblIDFld = 1;
this.tblNameFld = "test1";
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();
DataSource da = (DataSource) ctx.lookup("java:/XAPooledDataSource");
conn = da.getConnection();
Statement stmt = conn.createStatement();
String sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
stmt.executeUpdate(sqlSTR);
stmt.close();
ta.commit();
conn.close();
} catch (NamingException e) {
ta.rollback();
fail(" failed " + e.getMessage());
} catch (SQLException e) {
ta.rollback();
fail(" failed " + e.getMessage());
} catch (Exception e) {
ta.rollback();
fail(" failed " + e.getMessage());
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException ex) {
fail("SQL exception: " + ex.getMessage());
}
}
}
use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.
the class CacheJUnitTest method testScenario11.
/**
* Test of testScenario11 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
* Tests Simple DataSource lookup within a transaction. Things should not participate in the
* transaction.
*/
@Test
public void testScenario11() throws Exception {
this.tblIDFld = 11;
this.tblNameFld = "test11";
boolean rollback_chances = false;
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();
DataSource da = (DataSource) ctx.lookup("java:/SimpleDataSource");
conn = da.getConnection();
Statement stmt = conn.createStatement();
String sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
stmt.executeUpdate(sqlSTR);
rollback_chances = true;
// try insert the same data once more and see if all info gets rolled
// back...
// capable of throwing SQLException
sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
stmt.executeUpdate(sqlSTR);
stmt.close();
ta.commit();
conn.close();
} catch (NamingException e) {
ta.rollback();
} catch (SQLException e) {
if (rollback_chances) {
try {
ta.rollback();
} catch (Exception ex) {
fail("failed due to : " + ex.getMessage());
}
// try to check in the db whether any rows (the first one) are there
// now...
int ifAnyRows = jtaObj.getRows(this.tblName);
// one
assertEquals("first row not found in case of Simple Datasource", 1, ifAnyRows);
// row--
// the
// first
// one
// shud
// be
// there.
// checking
boolean matched = jtaObj.checkTableAgainstData(this.tblName, this.tblIDFld + "");
// the
// existence
// of
// first
// row
assertEquals("first row PK didn't matched", true, matched);
} else {
ta.rollback();
}
} catch (Exception e) {
fail(" failed due to: " + e.getMessage());
ta.rollback();
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
}
Aggregations