use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.
the class CacheJUnitTest method testScenario3.
/**
* Test of testScenario3 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. Put and get are done within the transaction block and also
* db updates are done. After committing we check whether commit is proper in db and also in
* Cache.
*/
@Test
public void testScenario3() throws Exception {
this.tblIDFld = 3;
this.tblNameFld = "test3";
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 {
/** begin the transaction **/
ta.begin();
String current_region = jtaObj.currRegion.getName();
assertEquals("the default region is not root", DEFAULT_RGN, current_region);
jtaObj.getRegionFromCache("region1");
String current_fullpath = jtaObj.currRegion.getFullPath();
assertEquals("failed retrieving current region fullpath", "/" + DEFAULT_RGN + "/region1", current_fullpath);
jtaObj.put("key1", "value1");
String str = jtaObj.get("key1");
String tok = jtaObj.parseGetValue(str);
assertEquals("get failed for corresponding 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();
Statement stmt = conn.createStatement();
String sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
stmt.executeUpdate(sqlSTR);
stmt.close();
ta.commit();
conn.close();
rollback_chances = false;
current_fullpath = jtaObj.currRegion.getFullPath();
assertEquals("failed retrieving current region fullpath after txn commit", "/" + DEFAULT_RGN + "/region1", current_fullpath);
int ifAnyRows = jtaObj.getRows(this.tblName);
assertEquals("rows retrieved is:" + ifAnyRows, 1, ifAnyRows);
/*
* if (ifAnyRows == 0) { fail (" DB FAILURE: no rows retrieved even after txn commit."); }
*/
// after jdbc commit cache value in region1 for key1 must retain...
str = jtaObj.get("key1");
tok = jtaObj.parseGetValue(str);
assertEquals("cache put didn't commit, value retrieved is: " + tok, "\"value1\"", tok);
} catch (CacheExistsException e) {
ta.rollback();
fail(" test 3 failed ");
} catch (NamingException e) {
if (rollback_chances)
ta.rollback();
fail(" test 3 failed " + e.getMessage());
} catch (SQLException e) {
if (rollback_chances)
ta.rollback();
fail(" test 3 failed " + e.getMessage());
} catch (Exception e) {
if (rollback_chances)
ta.rollback();
fail(" test 3 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 testScenario4.
/**
* Test of testScenario4 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. After rollback the transaction explicitly we
* check whether it was proper in db and also in Cache, which should also rollback.
*/
@Test
public void testScenario4() throws Exception {
this.tblIDFld = 4;
this.tblNameFld = "test4";
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 {
String current_region = jtaObj.currRegion.getName();
assertEquals("default region is not root", DEFAULT_RGN, current_region);
jtaObj.getRegionFromCache("region1");
String current_fullpath = jtaObj.currRegion.getFullPath();
assertEquals("failed retrieving current region fullpath after doing getRegionFromCache(region1)", "/" + 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 do not match with the 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();
Statement stmt = conn.createStatement();
String sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
stmt.executeUpdate(sqlSTR);
stmt.close();
ta.rollback();
conn.close();
rollback_chances = false;
current_fullpath = jtaObj.currRegion.getFullPath();
assertEquals("failed retirieving current region fullpath after txn rollback", "/" + DEFAULT_RGN + "/region1", current_fullpath);
int ifAnyRows = jtaObj.getRows(this.tblName);
assertEquals("rows retrieved is: " + ifAnyRows, 0, ifAnyRows);
/*
* if (ifAnyRows != 0) { fail (" DB FAILURE"); }
*/
str = jtaObj.get("key1");
tok = jtaObj.parseGetValue(str);
assertEquals("value existing in cache is: " + tok, "\"test\"", tok);
} catch (CacheExistsException e) {
ta.rollback();
fail(" failed " + e.getMessage());
} 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) {
if (rollback_chances)
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 testScenario18.
/**
* Test of testScenario18 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
* Get a connection (conn1) outside a transaction with pooled datasource lookup and another
* connection (conn2) within the same transaction with XAPooled datasource lookup. After making
* updates we do a rollback on the transaction and close both connections in the order of opening
* them. The connection opened within transaction will only participate in the transaction.
*/
@Test
public void testScenario18() throws Exception {
this.tblIDFld = 18;
this.tblNameFld = "test18";
boolean rollback_chances = true;
int ifAnyRows = 0;
JTAUtils jtaObj = new JTAUtils(cache, currRegion);
jtaObj.deleteRows(this.tblName);
Context ctx = cache.getJNDIContext();
// connection within txn
Connection conn2 = null;
DataSource da = (DataSource) ctx.lookup("java:/PooledDataSource");
// connection outside txn
Connection conn1 = da.getConnection();
UserTransaction ta = (UserTransaction) ctx.lookup("java:/UserTransaction");
try {
ta.begin();
Statement stmt = conn1.createStatement();
String sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
stmt.executeUpdate(sqlSTR);
stmt.close();
ifAnyRows = jtaObj.getRows(this.tblName);
if (ifAnyRows == 0) {
fail("failed no rows are there...");
}
// tryin a XAPooled lookup for second connection
da = (DataSource) ctx.lookup("java:/XAPooledDataSource");
conn2 = da.getConnection();
stmt = conn2.createStatement();
this.tblIDFld += 1;
sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
stmt.executeUpdate(sqlSTR);
// intensional rollback
ta.rollback();
stmt.close();
conn2.close();
conn1.close();
rollback_chances = false;
// if we reach here check whether the rollback was success for conn1 and
// conn2
ifAnyRows = jtaObj.getRows(this.tblName);
assertEquals("at least one row not retained after rollback", 1, ifAnyRows);
// checking
boolean matched = jtaObj.checkTableAgainstData(this.tblName, this.tblIDFld + "");
// field
if (matched) {
// data is still in db
fail(", PK " + this.tblIDFld + " found in db)" + " " + "rollback for conn #2 failed");
}
} finally {
if (conn1 != null)
try {
conn1.close();
} catch (SQLException ex) {
fail(" Exception: " + ex.getMessage());
}
if (conn2 != null)
try {
conn2.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 testScenario16.
/**
* Test of testScenario16 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
* Tests DDL operations on XAPooledDataSOurce lookup, without transaction but making auto commit
* false just before performing DDL queries and making it true before closing the Connection. The
* operations should be committed.
*/
@Test
public void testScenario16() throws Exception {
this.tblIDFld = 16;
this.tblNameFld = "test16";
String tbl = "";
int ddl_return = 1;
boolean row_num = true;
JTAUtils jtaObj = new JTAUtils(cache, currRegion);
jtaObj.deleteRows(this.tblName);
Context ctx = cache.getJNDIContext();
Connection conn = null;
Statement stmt = null;
try {
DataSource da = (DataSource) ctx.lookup("java:/XAPooledDataSource");
conn = da.getConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement();
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_key PRIMARY KEY(my_id))";
ddl_return = stmt.executeUpdate(sql);
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("table do not exists");
/*** 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.executeUpdate(sql); System.out.println (this.space + "ok");
*/
conn.setAutoCommit(true);
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)
try {
conn.close();
} catch (SQLException e) {
}
}
}
use of org.apache.geode.internal.jta.JTAUtils in project geode by apache.
the class CacheJUnitTest method testScenario9.
/**
* Test of testScenario9 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
* Tests a user transaction with cache lookup and with XAPooledDataSource lookup performing a db
* update within the transaction block and committing the transaction. Then agin we perform
* another db update with the same DataSource and finally close the Connection. The first update
* only should be committed in db and the second will not participate in transaction throwing an
* SQLException.
*/
@Test
public void testScenario9() throws Exception {
this.tblIDFld = 9;
this.tblNameFld = "test9";
boolean rollback_chances = true;
int first_field = this.tblIDFld;
JTAUtils jtaObj = new JTAUtils(cache, currRegion);
// delete the rows inserted from CacheUtils createTable, otherwise conflict
// in PK's
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);
ta.commit();
rollback_chances = false;
// intended test for failure-- capable of throwing SQLException
this.tblIDFld += 1;
sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
stmt.executeUpdate(sqlSTR);
stmt.close();
conn.close();
rollback_chances = true;
} catch (NamingException e) {
fail(" failed due to: " + e.getMessage());
ta.rollback();
} catch (SQLException e) {
if (!rollback_chances) {
int ifAnyRows = jtaObj.getRows(this.tblName);
assertEquals("rows found is: " + ifAnyRows, 1, ifAnyRows);
// first
boolean matched = jtaObj.checkTableAgainstData(this.tblName, first_field + "");
// field
// must
// be
// there.
assertEquals("first entry to db is not found", true, matched);
} else {
ta.rollback();
fail(" failed due to: " + e.getMessage());
}
} catch (Exception e) {
fail(" failed due to: " + e.getMessage());
if (rollback_chances) {
ta.rollback();
}
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
}
Aggregations