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();
}
}
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();
}
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();
}
}
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();
}
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();
}
Aggregations