blob: e16427ef1d7a1d82087c0d9e333a5f78b902e00c [file] [log] [blame]
/*=========================================================================
* Copyright (c) 2010-2014 Pivotal Software, Inc. All Rights Reserved.
* This product is protected by U.S. and international copyright
* and intellectual property laws. Pivotal products are covered by
* one or more patents listed at http://www.pivotal.io/patents.
*=========================================================================
*/
package com.gemstone.gemfire.internal.jta;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.ResultSet;
//import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.Random;
import javax.naming.Context;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;
import junit.framework.TestCase;
import org.junit.FixMethodOrder;
import org.junit.experimental.categories.Category;
import org.junit.runners.MethodSorters;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.distributed.DistributedSystem;
import com.gemstone.gemfire.internal.datasource.GemFireTransactionDataSource;
import com.gemstone.gemfire.util.test.TestUtil;
import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Category(IntegrationTest.class)
public class TransactionTimeOutJUnitTest extends TestCase {
private static Properties props = null;
private static DistributedSystem ds1 = null;
private static Cache cache = null;
protected void setUp() throws Exception {
super.setUp();
props = new Properties();
props.setProperty("mcast-port", "0");
int pid = new Random().nextInt();
File tmpFile = File.createTempFile("dunit-cachejta_", ".xml");
tmpFile.deleteOnExit();
String path = tmpFile.getAbsolutePath();
String file_as_str = readFile(TestUtil.getResourcePath(TransactionTimeOutJUnitTest.class, "/jta/cachejta.xml"));
String modified_file_str= file_as_str.replaceAll("newDB", "newDB_" + pid);
FileOutputStream fos = new FileOutputStream(path);
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(fos));
wr.write(modified_file_str);
wr.flush();
wr.close();
props.setProperty("cache-xml-file", path);
// props.setProperty("cache-xml-file","D:\\projects\\JTA\\cachejta.xml");
ds1 = DistributedSystem.connect(props);
cache = CacheFactory.create(ds1);
cache.getLogger().fine("SWAP:running test:"+getName());
}
protected void tearDown() throws Exception {
super.tearDown();
ds1.disconnect();
}
public TransactionTimeOutJUnitTest(String arg0) {
super(arg0);
}
public void test1One() {
try {
UserTransaction utx = new UserTransactionImpl();
utx.begin();
Thread.sleep(2000);
utx.setTransactionTimeout(2);
utx.setTransactionTimeout(200);
utx.setTransactionTimeout(3);
Thread.sleep(5000);
//utx.commit();
} catch (Exception e) {
fail("Exception in TestSetTransactionTimeOut due to " + e);
}
}
public void test2SetTransactionTimeOut() {
try {
UserTransaction utx = new UserTransactionImpl();
utx.begin();
utx.setTransactionTimeout(2);
System.out.println("Going to sleep");
Thread.sleep(6000);
utx.begin();
utx.commit();
} catch (Exception e) {
fail("Exception in TestSetTransactionTimeOut due to " + e);
}
}
public void test3ExceptionOnCommitAfterTimeOut() {
UserTransaction utx;
boolean exceptionOccured = true;
try {
utx = new UserTransactionImpl();
utx.setTransactionTimeout(2);
utx.begin();
Thread.sleep(4000);
try {
utx.commit();
} catch (Exception e) {
exceptionOccured = false;
}
if (exceptionOccured) {
fail("TimeOut did not rollback the transaction");
}
} catch (Exception e) {
fail("Exception in testExceptionOnCommitAfterTimeOut() due to " + e);
}
}
public void test4MultipleSetTimeOuts() {
UserTransaction utx;
boolean exceptionOccured = true;
try {
utx = new UserTransactionImpl();
utx.setTransactionTimeout(10);
utx.begin();
utx.setTransactionTimeout(8);
utx.setTransactionTimeout(6);
utx.setTransactionTimeout(2);
Thread.sleep(6000);
try {
utx.commit();
} catch (Exception e) {
exceptionOccured = false;
}
if (exceptionOccured) {
fail("TimeOut did not rollback the transaction");
}
} catch (Exception e) {
fail("Exception in testExceptionOnCommitAfterTimeOut() due to " + e);
}
}
public void test5TimeOutBeforeBegin() {
boolean exceptionOccured = true;
try {
UserTransaction utx = new UserTransactionImpl();
utx.setTransactionTimeout(4);
utx.begin();
Thread.sleep(6000);
try {
utx.commit();
} catch (Exception e) {
exceptionOccured = false;
}
if (exceptionOccured) {
fail("TimeOut did not rollback the transaction");
}
} catch (Exception e) {
fail("Exception in testExceptionOnCommitAfterTimeOut() due to " + e);
}
}
public void test6CommitBeforeTimeOut() {
// boolean exceptionOccured = true;
try {
UserTransaction utx = new UserTransactionImpl();
utx.begin();
utx.setTransactionTimeout(6);
Thread.sleep(2000);
try {
utx.commit();
} catch (Exception e) {
fail("Transaction failed to commit although TimeOut was not exceeded due to "
+ e);
}
} catch (Exception e) {
fail("Exception in testExceptionOnCommitAfterTimeOut() due to " + e);
}
}
public void test7Commit() {
try {
Context ctx = cache.getJNDIContext();
DataSource ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource");
ds2.getConnection();
GemFireTransactionDataSource ds = (GemFireTransactionDataSource) ctx
.lookup("java:/XAPooledDataSource");
UserTransaction utx = (UserTransaction) ctx
.lookup("java:/UserTransaction");
utx.begin();
Connection conn = ds.getConnection();
String sql = "create table newTable1 (id integer)";
Statement sm = conn.createStatement();
sm.execute(sql);
utx.setTransactionTimeout(30);
Thread.sleep(5000);
utx.setTransactionTimeout(20);
utx.setTransactionTimeout(10);
sql = "insert into newTable1 values (1)";
sm.execute(sql);
utx.commit();
sql = "select * from newTable1 where id = 1";
ResultSet rs = sm.executeQuery(sql);
if (!rs.next()) fail("Transaction not committed");
sql = "drop table newTable1";
sm.execute(sql);
sm.close();
conn.close();
} catch (Exception e) {
fail("Exception occured in test Commit due to " + e);
e.printStackTrace();
}
}
public void test8CommitAfterTimeOut() {
try {
boolean exceptionOccured = false;
Context ctx = cache.getJNDIContext();
DataSource ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource");
ds2.getConnection();
GemFireTransactionDataSource ds = (GemFireTransactionDataSource) ctx
.lookup("java:/XAPooledDataSource");
UserTransaction utx = (UserTransaction) ctx
.lookup("java:/UserTransaction");
utx.begin();
Connection conn = ds.getConnection();
String sql = "create table newTable2 (id integer)";
Statement sm = conn.createStatement();
sm.execute(sql);
utx.setTransactionTimeout(30);
sql = "insert into newTable2 values (1)";
sm.execute(sql);
sql = "select * from newTable2 where id = 1";
ResultSet rs = sm.executeQuery(sql);
if (!rs.next()) fail("Transaction not committed");
sql = "drop table newTable2";
sm.execute(sql);
sm.close();
conn.close();
utx.setTransactionTimeout(1);
Thread.sleep(3000);
try {
utx.commit();
} catch (Exception e) {
exceptionOccured = true;
}
if (!exceptionOccured) {
fail("exception did not occur on commit although transaction timed out");
}
} catch (Exception e) {
fail("Exception occured in test Commit due to " + e);
e.printStackTrace();
}
}
public void test9RollbackAfterTimeOut() {
try {
boolean exceptionOccured = false;
Context ctx = cache.getJNDIContext();
DataSource ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource");
Connection conn2 = ds2.getConnection();
GemFireTransactionDataSource ds = (GemFireTransactionDataSource) ctx
.lookup("java:/XAPooledDataSource");
UserTransaction utx = (UserTransaction) ctx
.lookup("java:/UserTransaction");
utx.begin();
Connection conn = ds.getConnection();
String sql = "create table newTable3 (id integer)";
Statement sm = conn.createStatement();
sm.execute(sql);
utx.setTransactionTimeout(30);
sql = "insert into newTable3 values (1)";
sm.execute(sql);
sql = "select * from newTable3 where id = 1";
ResultSet rs = sm.executeQuery(sql);
if (!rs.next()) fail("Transaction not committed");
sql = "drop table newTable3";
sm.execute(sql);
sm.close();
conn.close();
conn2.close();
utx.setTransactionTimeout(1);
Thread.sleep(3000);
try {
utx.rollback();
} catch (Exception e) {
exceptionOccured = true;
}
if (!exceptionOccured) {
fail("exception did not occur on rollback although transaction timed out");
}
} catch (Exception e) {
e.printStackTrace();
fail("Exception occured in test Commit due to " + e);
}
}
private static String readFile(String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String nextLine = "";
StringBuffer sb = new StringBuffer();
while ((nextLine = br.readLine()) != null) {
sb.append(nextLine);
//
// note:
// BufferedReader strips the EOL character.
//
// sb.append(lineSep);
}
return sb.toString();
}
}