Search in sources :

Example 1 with DummyXAResource

use of com.hp.mwtests.ts.jta.commitmarkable.DummyXAResource in project narayana by jbosstm.

the class CMRIntegrationTest method doTest.

public void doTest(final DataSource dataSource) throws Exception {
    // Test code
    Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new Thread(new Runnable() {

            public void run() {
                synchronized (waitLock) {
                    waiting++;
                    waitLock.notify();
                }
                synchronized (CMRIntegrationTest.this) {
                    while (!go) {
                        try {
                            CMRIntegrationTest.this.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            return;
                        }
                    }
                }
                int success = 0;
                Connection connection = null;
                int faultType = Integer.getInteger("com.hp.mwtests.ts.jta.commitmarkable.integration.CMRIntegrationTest", 0);
                for (int i = 0; i < iterationCount; i++) {
                    try {
                        userTransaction.begin();
                        tm.getTransaction().enlistResource(new DummyXAResource());
                        connection = dataSource.getConnection();
                        Statement createStatement = connection.createStatement();
                        createStatement.execute("INSERT INTO foo (bar) VALUES (1)");
                        if (faultType == 1)
                            Runtime.getRuntime().halt(0);
                        userTransaction.commit();
                        // This wouldn't work for a
                        connection.close();
                        // none-JCA code as commit has
                        // closed the connection - it
                        // helps us though as JCA seems
                        // to rely on finalize
                        // System.out
                        // .printf("committed txn iteration %d%n", i);
                        success++;
                    } catch (SQLException e) {
                        System.err.println("boom");
                        e.printStackTrace();
                        if (e.getCause() != null) {
                            e.getCause().printStackTrace();
                        }
                        SQLException nextException = e.getNextException();
                        while (nextException != null) {
                            nextException.printStackTrace();
                            nextException = nextException.getNextException();
                        }
                        Throwable[] suppressed = e.getSuppressed();
                        for (int j = 0; j < suppressed.length; j++) {
                            suppressed[j].printStackTrace();
                        }
                        try {
                            userTransaction.rollback();
                        } catch (IllegalStateException | SecurityException | SystemException e1) {
                            e1.printStackTrace();
                            fail("Problem with transaction");
                        }
                    } catch (NotSupportedException | SystemException | IllegalStateException | RollbackException | SecurityException | HeuristicMixedException | HeuristicRollbackException e) {
                        e.printStackTrace();
                        fail("Problem with transaction");
                    } finally {
                        if (connection != null)
                            try {
                                connection.close();
                            } catch (SQLException e) {
                                // To change body of
                                e.printStackTrace();
                            // catch statement
                            // use File |
                            // Settings | File
                            // Templates.
                            }
                    }
                }
                totalExecuted.addAndGet(success);
            }
        });
        threads[i].start();
    }
    synchronized (waitLock) {
        while (waiting < threads.length) {
            waitLock.wait();
        }
    }
    long startTime = -1;
    synchronized (CMRIntegrationTest.this) {
        go = true;
        CMRIntegrationTest.this.notifyAll();
        startTime = System.currentTimeMillis();
    }
    for (int i = 0; i < threads.length; i++) {
        threads[i].join();
    }
    long endTime = System.currentTimeMillis();
    System.out.println(new Date() + "  Number of transactions: " + totalExecuted.intValue());
    // postRunCleanup(dataSource);
    long additionalCleanuptime = 0L;
    long timeInMillis = (endTime - startTime) + additionalCleanuptime;
    System.out.printf("  Total time millis: %d%n", timeInMillis);
    System.out.printf("  Average transaction time: %d%n", timeInMillis / totalExecuted.intValue());
    System.out.printf("  Transactions per second: %d%n", Math.round((totalExecuted.intValue() / (timeInMillis / 1000d))));
    checkFooSize(dataSource);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) Date(java.util.Date) DummyXAResource(com.hp.mwtests.ts.jta.commitmarkable.DummyXAResource) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException)

Example 2 with DummyXAResource

use of com.hp.mwtests.ts.jta.commitmarkable.DummyXAResource in project narayana by jbosstm.

the class CMRIntegrationTest method testCMR1.

// @Test
public void testCMR1() throws Exception {
    Utils.createTables(ds.getConnection());
    try {
        userTransaction.begin();
        tm.getTransaction().enlistResource(new DummyXAResource());
        Connection connection = ds.getConnection();
        Statement createStatement = connection.createStatement();
        createStatement.execute("INSERT INTO foo (bar) VALUES (1)");
        createStatement.close();
        userTransaction.commit();
    } catch (Exception e) {
        System.out.printf("XXX txn excp: %s%n", e.getCause());
    } finally {
        try {
            if (userTransaction.getStatus() == Status.STATUS_ACTIVE || userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK)
                userTransaction.rollback();
        } catch (Throwable e) {
            System.out.printf("XXX txn did not finish: %s%n", e.getCause());
        }
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) ObjectStoreException(com.arjuna.ats.arjuna.exceptions.ObjectStoreException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) DummyXAResource(com.hp.mwtests.ts.jta.commitmarkable.DummyXAResource)

Aggregations

DummyXAResource (com.hp.mwtests.ts.jta.commitmarkable.DummyXAResource)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 HeuristicMixedException (javax.transaction.HeuristicMixedException)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 NotSupportedException (javax.transaction.NotSupportedException)2 RollbackException (javax.transaction.RollbackException)2 SystemException (javax.transaction.SystemException)2 ObjectStoreException (com.arjuna.ats.arjuna.exceptions.ObjectStoreException)1 Date (java.util.Date)1