Search in sources :

Example 56 with Synchronization

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

the class GlobalTransactionJUnitTest method testRegisterSynchronization.

@Test
public void testRegisterSynchronization() throws Exception {
    try {
        boolean exceptionoccurred = false;
        utx.begin();
        try {
            Transaction txn = tm.getTransaction();
            Synchronization sync = new SyncImpl();
            txn.registerSynchronization(sync);
        } catch (RollbackException e) {
            exceptionoccurred = true;
        }
        if (exceptionoccurred)
            fail("exception occurred while trying to register synchronization ");
        utx.rollback();
    } catch (Exception e) {
        try {
            utx.rollback();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        fail("exception in testRegisterSynchronization due to " + e);
        e.printStackTrace();
    }
}
Also used : Transaction(javax.transaction.Transaction) UserTransaction(javax.transaction.UserTransaction) Synchronization(javax.transaction.Synchronization) RollbackException(javax.transaction.RollbackException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 57 with Synchronization

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

the class TransactionImplJUnitTest method testRegisterSynchronization.

@Test
public void testRegisterSynchronization() throws Exception {
    utx.begin();
    TransactionImpl txn = (TransactionImpl) tm.getTransaction();
    Synchronization sync = new SyncImpl();
    txn.registerSynchronization(sync);
    assertTrue("Synchronization not registered succesfully", txn.getSyncList().contains(sync));
    utx.commit();
}
Also used : Synchronization(javax.transaction.Synchronization) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 58 with Synchronization

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

the class GlobalTransactionJUnitTest method testRegisterSynchronizationAfterRollBack.

@Test
public void testRegisterSynchronizationAfterRollBack() throws Exception {
    try {
        boolean exceptionoccurred = false;
        utx.begin();
        utx.setRollbackOnly();
        Context ctx = cache.getJNDIContext();
        try {
            Transaction txn = tm.getTransaction();
            Synchronization sync = new SyncImpl();
            txn.registerSynchronization(sync);
        } catch (RollbackException e) {
            exceptionoccurred = true;
        }
        if (!exceptionoccurred)
            fail("RollbackException not occurred although the transaction was marked for rollback");
        utx.rollback();
    } catch (Exception e) {
        try {
            utx.rollback();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        fail("exception in testSetRollbackonly due to " + e);
        e.printStackTrace();
    }
}
Also used : Context(javax.naming.Context) Transaction(javax.transaction.Transaction) UserTransaction(javax.transaction.UserTransaction) Synchronization(javax.transaction.Synchronization) RollbackException(javax.transaction.RollbackException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 59 with Synchronization

use of javax.transaction.Synchronization in project narayana by jbosstm.

the class ConnectionImpleTest method closeConnectionInAfterCompletion.

@Test
public void closeConnectionInAfterCompletion() throws Exception {
    transactionManager.begin();
    // Normally driver does this
    transactionManager.getTransaction().enlistResource(xaResource);
    ConnectionImple connectionToTest = getConnectionToTest();
    // Initialises the connection
    connectionToTest.clearWarnings();
    transactionManager.getTransaction().registerSynchronization(new Synchronization() {

        @Override
        public void beforeCompletion() {
        }

        @Override
        public void afterCompletion(int status) {
            try {
                connectionToTest.close();
            } catch (SQLException e) {
                fail("Could not close the connection: " + e);
            }
        }
    });
    transactionManager.commit();
    verify(connection, times(1)).close();
}
Also used : SQLException(java.sql.SQLException) Synchronization(javax.transaction.Synchronization) ConnectionImple(com.arjuna.ats.internal.jdbc.ConnectionImple) Test(org.junit.Test)

Example 60 with Synchronization

use of javax.transaction.Synchronization in project narayana by jbosstm.

the class JDBC2Test method testBC.

@Test
public void testBC() throws SystemException, NotSupportedException, SQLException, HeuristicRollbackException, HeuristicMixedException, RollbackException {
    try (Statement stmt = conn.createStatement()) {
        try {
            stmt.executeUpdate("DROP TABLE test_table");
        } catch (Exception e) {
        // Ignore
        } finally {
            stmt.executeUpdate("CREATE TABLE test_table (a INTEGER,b INTEGER)");
        }
    }
    javax.transaction.TransactionManager tx = com.arjuna.ats.jta.TransactionManager.transactionManager();
    tx.begin();
    TransactionSynchronizationRegistryImple transactionSynchronizationRegistryImple = new TransactionSynchronizationRegistryImple();
    transactionSynchronizationRegistryImple.registerInterposedSynchronization(new Synchronization() {

        @Override
        public void beforeCompletion() {
            try {
                ResultSet res = conn.prepareStatement("SELECT * FROM test_table").executeQuery();
                int rowCount1 = 0;
                while (res.next()) {
                    rowCount1++;
                }
                conn.createStatement().execute("INSERT INTO test_table (a, b) VALUES (1,2)");
                res = conn.prepareStatement("SELECT * FROM test_table").executeQuery();
                int rowCount2 = 0;
                while (res.next()) {
                    rowCount2++;
                }
                conn.close();
                if (rowCount2 != rowCount1 + 1) {
                    fail("Number of rows = " + rowCount2 + ", test was expecting " + rowCount1 + 1);
                }
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }

        @Override
        public void afterCompletion(int status) {
        }
    });
    tx.commit();
}
Also used : TransactionSynchronizationRegistryImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Synchronization(javax.transaction.Synchronization) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NotSupportedException(javax.transaction.NotSupportedException) SQLException(java.sql.SQLException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) Test(org.junit.Test)

Aggregations

Synchronization (javax.transaction.Synchronization)87 Test (org.junit.Test)42 Transaction (javax.transaction.Transaction)23 SystemException (javax.transaction.SystemException)21 RollbackException (javax.transaction.RollbackException)14 SQLException (java.sql.SQLException)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)7 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 UserTransaction (javax.transaction.UserTransaction)5 XAResource (javax.transaction.xa.XAResource)5 EJBException (javax.ejb.EJBException)4 HeuristicMixedException (javax.transaction.HeuristicMixedException)4 TransactionImple (com.arjuna.ats.internal.jta.transaction.jts.TransactionImple)3 List (java.util.List)3 Xid (javax.transaction.xa.Xid)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3