Search in sources :

Example 41 with UserTransaction

use of javax.transaction.UserTransaction in project geode by apache.

the class ExceptionsDUnitTest method runTest3.

public static void runTest3() throws Exception {
    boolean exceptionOccurred = false;
    try {
        Context ctx = cache.getJNDIContext();
        DataSource ds1 = null;
        DataSource ds2 = null;
        ds1 = (DataSource) ctx.lookup("java:/XAPooledDataSource");
        ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource");
        ds2.getConnection();
        UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
        utx.begin();
        utx.setTransactionTimeout(2);
        ds1.getConnection();
        Thread.sleep(4000);
        try {
            utx.commit();
        } catch (Exception e) {
            exceptionOccurred = true;
        }
        if (!exceptionOccurred)
            fail("Exception (Transaction-Time-Out)did not occur although was supposed" + "to occur");
    } catch (Exception e) {
        fail("failed in runTest3 due to " + e);
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) SQLException(java.sql.SQLException) IOException(java.io.IOException) DataSource(javax.sql.DataSource)

Example 42 with UserTransaction

use of javax.transaction.UserTransaction in project geode by apache.

the class ExceptionsDUnitTest method runTest2.

public static void runTest2() throws Exception {
    boolean exceptionOccurred1 = false;
    boolean exceptionOccurred2 = false;
    try {
        Context ctx = cache.getJNDIContext();
        DataSource ds1 = null;
        DataSource ds2 = null;
        ds1 = (DataSource) ctx.lookup("java:/XAPooledDataSource");
        ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource");
        ds2.getConnection();
        ds1.getConnection();
        ds1.getConnection();
        ds1.getConnection();
        ds1.getConnection();
        ds1.getConnection();
        UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
        utx.begin();
        try {
            ds1.getConnection();
            Thread.sleep(8000);
        } catch (SQLException e) {
            exceptionOccurred1 = true;
        }
        try {
            utx.commit();
        } catch (Exception e) {
            exceptionOccurred2 = true;
        }
        if (!exceptionOccurred1)
            fail("Exception (Login-Time-Out)did not occur although was supposed" + "to occur");
        if (exceptionOccurred2)
            fail("Exception did occur on commit, although was not supposed" + "to occur");
    } catch (Exception e) {
        fail("failed in runTest2 due to " + e);
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) SQLException(java.sql.SQLException) SQLException(java.sql.SQLException) IOException(java.io.IOException) DataSource(javax.sql.DataSource)

Example 43 with UserTransaction

use of javax.transaction.UserTransaction in project geode by apache.

the class TransactionTimeOutJUnitTest method test1One.

@Test
public void test1One() throws Exception {
    UserTransaction utx = new UserTransactionImpl();
    utx.begin();
    Thread.sleep(2000);
    utx.setTransactionTimeout(2);
    utx.setTransactionTimeout(200);
    utx.setTransactionTimeout(3);
    Thread.sleep(5000);
// utx.commit();
}
Also used : UserTransaction(javax.transaction.UserTransaction) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 44 with UserTransaction

use of javax.transaction.UserTransaction in project geode by apache.

the class TransactionTimeOutJUnitTest method test2SetTransactionTimeOut.

@Test
public void test2SetTransactionTimeOut() throws Exception {
    UserTransaction utx = new UserTransactionImpl();
    utx.begin();
    utx.setTransactionTimeout(2);
    System.out.println("Going to sleep");
    Thread.sleep(6000);
    utx.begin();
    utx.commit();
}
Also used : UserTransaction(javax.transaction.UserTransaction) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 45 with UserTransaction

use of javax.transaction.UserTransaction in project geode by apache.

the class TransactionTimeOutJUnitTest method test8CommitAfterTimeOut.

@Test
public void test8CommitAfterTimeOut() throws Exception {
    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();
        fail("exception did not occur on commit although transaction timed out");
    } catch (Exception expected) {
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) GemFireTransactionDataSource(org.apache.geode.internal.datasource.GemFireTransactionDataSource) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IOException(java.io.IOException) DataSource(javax.sql.DataSource) GemFireTransactionDataSource(org.apache.geode.internal.datasource.GemFireTransactionDataSource) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

UserTransaction (javax.transaction.UserTransaction)224 Test (org.junit.Test)105 Context (javax.naming.Context)47 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)46 Node (javax.jcr.Node)42 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)42 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)42 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)35 DataSource (javax.sql.DataSource)30 Connection (java.sql.Connection)27 NamingException (javax.naming.NamingException)25 IOException (java.io.IOException)24 Statement (java.sql.Statement)23 Session (javax.jcr.Session)23 InitialContext (javax.naming.InitialContext)22 SQLException (java.sql.SQLException)20 SystemException (javax.transaction.SystemException)17 RollbackException (javax.transaction.RollbackException)16 Region (org.apache.geode.cache.Region)13 Lock (javax.jcr.lock.Lock)12