use of javax.naming.Context in project geode by apache.
the class IdleTimeOutDUnitTest method createTable.
public static void createTable(String tableName) throws NamingException, SQLException {
Context ctx = cache.getJNDIContext();
DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource");
// String sql = "create table " + tableName + " (id number primary key, name
// varchar2(50))";
// String sql = "create table " + tableName + " (id integer primary key,
// name varchar(50))";
String sql = "create table " + tableName + " (id integer NOT NULL, name varchar(50), CONSTRAINT " + tableName + "_key PRIMARY KEY(id))";
LogWriterUtils.getLogWriter().info(sql);
Connection conn = ds.getConnection();
Statement sm = conn.createStatement();
sm.execute(sql);
sm.close();
sm = conn.createStatement();
for (int i = 1; i <= 10; i++) {
sql = "insert into " + tableName + " values (" + i + ",'name" + i + "')";
sm.addBatch(sql);
LogWriterUtils.getLogWriter().info(sql);
}
sm.executeBatch();
conn.close();
}
use of javax.naming.Context in project geode by apache.
the class CacheUtils method destroyTable.
public static void destroyTable(String tableName) throws NamingException, SQLException {
Context ctx = cache.getJNDIContext();
DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource");
Connection conn = ds.getConnection();
// System.out.println (" trying to drop table: " + tableName);
String sql = "drop table " + tableName;
Statement sm = conn.createStatement();
sm.execute(sql);
conn.close();
}
use of javax.naming.Context 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();
}
}
use of javax.naming.Context 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();
}
}
use of javax.naming.Context 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");
}
Aggregations