Search in sources :

Example 6 with LocalServer

use of com.arjuna.ats.jta.distributed.server.LocalServer in project narayana by jbosstm.

the class SimpleIsolatedServers method testUnPreparedRollback.

@Test
public void testUnPreparedRollback() throws Exception {
    System.out.println("testUnPreparedRollback");
    LocalServer originalServer = getLocalServer("1000");
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(originalServer.getClassLoader());
    TransactionManager transactionManager = originalServer.getTransactionManager();
    transactionManager.setTransactionTimeout(0);
    transactionManager.begin();
    Transaction originalTransaction = transactionManager.getTransaction();
    int remainingTimeout = (int) (originalServer.getTimeLeftBeforeTransactionTimeout() / 1000);
    Xid currentXid = originalServer.getCurrentXid();
    transactionManager.suspend();
    DataReturnedFromRemoteServer performTransactionalWork = performTransactionalWork(new LinkedList<String>(Arrays.asList(new String[] { "2000" })), remainingTimeout, currentXid, 1, false, false);
    transactionManager.resume(originalTransaction);
    XAResource proxyXAResource = originalServer.generateProxyXAResource("2000", performTransactionalWork.getProxyRequired());
    originalTransaction.enlistResource(proxyXAResource);
    originalTransaction.registerSynchronization(originalServer.generateProxySynchronization("2000", currentXid));
    transactionManager.rollback();
    Thread.currentThread().setContextClassLoader(classLoader);
    assertTrue("" + completionCounter.getCommitCount("1000"), completionCounter.getCommitCount("1000") == 0);
    assertTrue("" + completionCounter.getCommitCount("2000"), completionCounter.getCommitCount("2000") == 0);
    assertTrue("" + completionCounter.getRollbackCount("2000"), completionCounter.getRollbackCount("2000") == 1);
    assertTrue("" + completionCounter.getRollbackCount("1000"), completionCounter.getRollbackCount("1000") == 1);
}
Also used : LocalServer(com.arjuna.ats.jta.distributed.server.LocalServer) Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) IsolatableServersClassLoader(com.arjuna.ats.jta.distributed.server.IsolatableServersClassLoader) Test(org.junit.Test)

Example 7 with LocalServer

use of com.arjuna.ats.jta.distributed.server.LocalServer in project narayana by jbosstm.

the class SimpleIsolatedServers method performTransactionalWork.

private DataReturnedFromRemoteServer performTransactionalWork(List<String> nodesToFlowTo, int remainingTimeout, Xid toMigrate, int numberOfResourcesToRegister, boolean addSynchronization, boolean rollbackOnlyOnLastNode) throws RollbackException, IllegalStateException, XAException, SystemException, NotSupportedException, IOException {
    String currentServerName = nodesToFlowTo.remove(0);
    LocalServer currentServer = getLocalServer(currentServerName);
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(currentServer.getClassLoader());
    Xid requiresProxyAtPreviousServer = currentServer.locateOrImportTransactionThenResumeIt(remainingTimeout, toMigrate);
    // Perform work on the migrated transaction
    {
        TransactionManager transactionManager = currentServer.getTransactionManager();
        Transaction transaction = transactionManager.getTransaction();
        if (addSynchronization) {
            transaction.registerSynchronization(new TestSynchronization(currentServer.getNodeName()));
        }
        for (int i = 0; i < numberOfResourcesToRegister; i++) {
            transaction.enlistResource(new TestResource(currentServer.getNodeName(), false));
        }
        if (rollbackOnlyOnLastNode && nodesToFlowTo.isEmpty()) {
            transaction.setRollbackOnly();
        }
    }
    if (!nodesToFlowTo.isEmpty()) {
        TransactionManager transactionManager = currentServer.getTransactionManager();
        Transaction transaction = transactionManager.getTransaction();
        int status = transaction.getStatus();
        // the transaction reaper
        if (status == Status.STATUS_ACTIVE) {
            String nextServerNodeName = nodesToFlowTo.get(0);
            // FLOW THE TRANSACTION
            remainingTimeout = (int) (currentServer.getTimeLeftBeforeTransactionTimeout() / 1000);
            // STORE AND SUSPEND THE TRANSACTION
            Xid currentXid = currentServer.getCurrentXid();
            transactionManager.suspend();
            DataReturnedFromRemoteServer dataReturnedFromRemoteServer = performTransactionalWork(nodesToFlowTo, remainingTimeout, currentXid, numberOfResourcesToRegister, addSynchronization, rollbackOnlyOnLastNode);
            transactionManager.resume(transaction);
            // transactions and performance issues
            if (dataReturnedFromRemoteServer.getProxyRequired() != null) {
                XAResource proxyXAResource = currentServer.generateProxyXAResource(nextServerNodeName, dataReturnedFromRemoteServer.getProxyRequired());
                transaction.enlistResource(proxyXAResource);
                transaction.registerSynchronization(currentServer.generateProxySynchronization(nextServerNodeName, toMigrate));
            }
            // from the subordinate
            switch(dataReturnedFromRemoteServer.getTransactionState()) {
                case Status.STATUS_MARKED_ROLLBACK:
                case Status.STATUS_ROLLEDBACK:
                case Status.STATUS_ROLLING_BACK:
                    switch(transaction.getStatus()) {
                        case Status.STATUS_MARKED_ROLLBACK:
                        case Status.STATUS_ROLLEDBACK:
                        case Status.STATUS_ROLLING_BACK:
                            transaction.setRollbackOnly();
                    }
                    break;
                default:
                    break;
            }
        }
    }
    TransactionManager transactionManager = currentServer.getTransactionManager();
    int transactionState = transactionManager.getStatus();
    // SUSPEND THE TRANSACTION WHEN YOU ARE READY TO RETURN TO YOUR CALLER
    transactionManager.suspend();
    // Return to the previous caller back over the transport/classloader
    // boundary in this case
    Thread.currentThread().setContextClassLoader(classLoader);
    return new DataReturnedFromRemoteServer(requiresProxyAtPreviousServer, transactionState);
}
Also used : LocalServer(com.arjuna.ats.jta.distributed.server.LocalServer) Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) IsolatableServersClassLoader(com.arjuna.ats.jta.distributed.server.IsolatableServersClassLoader)

Example 8 with LocalServer

use of com.arjuna.ats.jta.distributed.server.LocalServer in project narayana by jbosstm.

the class SimpleIsolatedServers method performTransactionalWork2.

private DataReturnedFromRemoteServer performTransactionalWork2(List<String> nodesToFlowTo, int remainingTimeout, Xid toMigrate, int numberOfResourcesToRegister, boolean addSynchronization, boolean rollbackOnlyOnLastNode) throws RollbackException, IllegalStateException, XAException, SystemException, NotSupportedException, IOException {
    String currentServerName = nodesToFlowTo.remove(0);
    LocalServer currentServer = getLocalServer(currentServerName);
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(currentServer.getClassLoader());
    Xid requiresProxyAtPreviousServer = currentServer.locateOrImportTransactionThenResumeIt(remainingTimeout, toMigrate);
    if (!currentServerName.equals("1000")) {
        TransactionManager transactionManager = currentServer.getTransactionManager();
        Transaction transaction = transactionManager.getTransaction();
        if (addSynchronization) {
            transaction.registerSynchronization(new TestSynchronization(currentServer.getNodeName()));
        }
        for (int i = 0; i < numberOfResourcesToRegister; i++) {
            transaction.enlistResource(new TestResource(currentServer.getNodeName(), false, currentServerName.equals("2000")));
        }
        if (rollbackOnlyOnLastNode && nodesToFlowTo.isEmpty()) {
            transaction.setRollbackOnly();
        }
    }
    if (!nodesToFlowTo.isEmpty()) {
        TransactionManager transactionManager = currentServer.getTransactionManager();
        Transaction transaction = transactionManager.getTransaction();
        int status = transaction.getStatus();
        // the transaction reaper
        if (status == Status.STATUS_ACTIVE) {
            String nextServerNodeName = nodesToFlowTo.get(0);
            // FLOW THE TRANSACTION
            remainingTimeout = (int) (currentServer.getTimeLeftBeforeTransactionTimeout() / 1000);
            // STORE AND SUSPEND THE TRANSACTION
            Xid currentXid = currentServer.getCurrentXid();
            transactionManager.suspend();
            DataReturnedFromRemoteServer dataReturnedFromRemoteServer = performTransactionalWork2(nodesToFlowTo, remainingTimeout, currentXid, numberOfResourcesToRegister, addSynchronization, rollbackOnlyOnLastNode);
            transactionManager.resume(transaction);
            // transactions and performance issues
            if (dataReturnedFromRemoteServer.getProxyRequired() != null) {
                XAResource proxyXAResource = currentServer.generateProxyXAResource(nextServerNodeName, dataReturnedFromRemoteServer.getProxyRequired(), true);
                transaction.enlistResource(proxyXAResource);
                transaction.registerSynchronization(currentServer.generateProxySynchronization(nextServerNodeName, toMigrate));
            }
            if (currentServerName.equals("1000")) {
                if (addSynchronization) {
                    transaction.registerSynchronization(new TestSynchronization(currentServer.getNodeName()));
                }
                for (int i = 0; i < numberOfResourcesToRegister; i++) {
                    transaction.enlistResource(new TestResource(currentServer.getNodeName(), false));
                }
                if (rollbackOnlyOnLastNode && nodesToFlowTo.isEmpty()) {
                    transaction.setRollbackOnly();
                }
            }
            // from the subordinate
            switch(dataReturnedFromRemoteServer.getTransactionState()) {
                case Status.STATUS_MARKED_ROLLBACK:
                case Status.STATUS_ROLLEDBACK:
                case Status.STATUS_ROLLING_BACK:
                    switch(transaction.getStatus()) {
                        case Status.STATUS_MARKED_ROLLBACK:
                        case Status.STATUS_ROLLEDBACK:
                        case Status.STATUS_ROLLING_BACK:
                            transaction.setRollbackOnly();
                    }
                    break;
                default:
                    break;
            }
        }
    }
    TransactionManager transactionManager = currentServer.getTransactionManager();
    int transactionState = transactionManager.getStatus();
    // SUSPEND THE TRANSACTION WHEN YOU ARE READY TO RETURN TO YOUR CALLER
    transactionManager.suspend();
    // Return to the previous caller back over the transport/classloader
    // boundary in this case
    Thread.currentThread().setContextClassLoader(classLoader);
    return new DataReturnedFromRemoteServer(requiresProxyAtPreviousServer, transactionState);
}
Also used : LocalServer(com.arjuna.ats.jta.distributed.server.LocalServer) Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) IsolatableServersClassLoader(com.arjuna.ats.jta.distributed.server.IsolatableServersClassLoader)

Example 9 with LocalServer

use of com.arjuna.ats.jta.distributed.server.LocalServer in project narayana by jbosstm.

the class SimpleIsolatedServers method testOnePhaseCommit.

@Test
public void testOnePhaseCommit() throws Exception {
    System.out.println("testOnePhaseCommit");
    LocalServer originalServer = getLocalServer("1000");
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(originalServer.getClassLoader());
    TransactionManager transactionManager = originalServer.getTransactionManager();
    transactionManager.setTransactionTimeout(0);
    transactionManager.begin();
    Transaction originalTransaction = transactionManager.getTransaction();
    int remainingTimeout = (int) (originalServer.getTimeLeftBeforeTransactionTimeout() / 1000);
    Xid currentXid = originalServer.getCurrentXid();
    transactionManager.suspend();
    DataReturnedFromRemoteServer performTransactionalWork = performTransactionalWork(new LinkedList<String>(Arrays.asList(new String[] { "2000" })), remainingTimeout, currentXid, 1, false, false);
    transactionManager.resume(originalTransaction);
    XAResource proxyXAResource = originalServer.generateProxyXAResource("2000", performTransactionalWork.getProxyRequired());
    originalTransaction.enlistResource(proxyXAResource);
    transactionManager.commit();
    Thread.currentThread().setContextClassLoader(classLoader);
    assertTrue("" + completionCounter.getCommitCount("1000"), completionCounter.getCommitCount("1000") == 1);
    assertTrue("" + completionCounter.getCommitCount("2000"), completionCounter.getCommitCount("2000") == 1);
    assertTrue("" + completionCounter.getRollbackCount("2000"), completionCounter.getRollbackCount("2000") == 0);
    assertTrue("" + completionCounter.getRollbackCount("1000"), completionCounter.getRollbackCount("1000") == 0);
}
Also used : LocalServer(com.arjuna.ats.jta.distributed.server.LocalServer) Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) IsolatableServersClassLoader(com.arjuna.ats.jta.distributed.server.IsolatableServersClassLoader) Test(org.junit.Test)

Example 10 with LocalServer

use of com.arjuna.ats.jta.distributed.server.LocalServer in project narayana by jbosstm.

the class SimpleIsolatedServers method doRecursiveTransactionalWork2.

private void doRecursiveTransactionalWork2(int startingTimeout, List<String> nodesToFlowTo, boolean commit, boolean rollbackOnlyOnLastNode) throws Exception {
    List<String> uniqueServers = new ArrayList<String>();
    Iterator<String> iterator = nodesToFlowTo.iterator();
    while (iterator.hasNext()) {
        String intern = iterator.next().intern();
        if (!uniqueServers.contains(intern)) {
            uniqueServers.add(intern);
        }
    }
    // Start out at the first server
    int totalCompletionCount = nodesToFlowTo.size() + uniqueServers.size() - 1;
    String startingServer = nodesToFlowTo.get(0);
    LocalServer originalServer = getLocalServer(startingServer);
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(originalServer.getClassLoader());
    TransactionManager transactionManager = originalServer.getTransactionManager();
    transactionManager.setTransactionTimeout(startingTimeout);
    transactionManager.begin();
    Transaction transaction = transactionManager.getTransaction();
    int remainingTimeout = (int) (originalServer.getTimeLeftBeforeTransactionTimeout() / 1000);
    Xid currentXid = originalServer.getCurrentXid();
    transactionManager.suspend();
    DataReturnedFromRemoteServer dataReturnedFromRemoteServer = performTransactionalWork2(nodesToFlowTo, remainingTimeout, currentXid, 1, true, rollbackOnlyOnLastNode);
    transactionManager.resume(transaction);
    // from the subordinate
    switch(dataReturnedFromRemoteServer.getTransactionState()) {
        case Status.STATUS_MARKED_ROLLBACK:
        case Status.STATUS_ROLLEDBACK:
        case Status.STATUS_ROLLING_BACK:
            switch(transaction.getStatus()) {
                case Status.STATUS_MARKED_ROLLBACK:
                case Status.STATUS_ROLLEDBACK:
                case Status.STATUS_ROLLING_BACK:
                    transaction.setRollbackOnly();
            }
            break;
        default:
            break;
    }
    if (commit) {
        try {
            transactionManager.commit();
            assertTrue("" + completionCounter.getTotalCommitCount(), completionCounter.getTotalCommitCount() == 2);
        } catch (RollbackException e) {
            if (!rollbackOnlyOnLastNode) {
                assertTrue(completionCounter.getTotalRollbackCount() == totalCompletionCount);
            }
        }
    } else {
        transactionManager.rollback();
        assertTrue(completionCounter.getTotalRollbackCount() == totalCompletionCount);
    }
    Thread.currentThread().setContextClassLoader(classLoader);
}
Also used : LocalServer(com.arjuna.ats.jta.distributed.server.LocalServer) Xid(javax.transaction.xa.Xid) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) ArrayList(java.util.ArrayList) IsolatableServersClassLoader(com.arjuna.ats.jta.distributed.server.IsolatableServersClassLoader) RollbackException(javax.transaction.RollbackException)

Aggregations

IsolatableServersClassLoader (com.arjuna.ats.jta.distributed.server.IsolatableServersClassLoader)10 LocalServer (com.arjuna.ats.jta.distributed.server.LocalServer)10 Transaction (javax.transaction.Transaction)10 TransactionManager (javax.transaction.TransactionManager)10 Xid (javax.transaction.xa.Xid)10 XAResource (javax.transaction.xa.XAResource)8 Test (org.junit.Test)6 RollbackException (javax.transaction.RollbackException)4 ArrayList (java.util.ArrayList)2 InputStream (java.io.InputStream)1 Date (java.util.Date)1 XAException (javax.transaction.xa.XAException)1 BMScript (org.jboss.byteman.contrib.bmunit.BMScript)1 ExecuteException (org.jboss.byteman.rule.exception.ExecuteException)1