Example usage for javax.transaction TransactionManager commit

List of usage examples for javax.transaction TransactionManager commit

Introduction

In this page you can find the example usage for javax.transaction TransactionManager commit.

Prototype

public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
        SecurityException, IllegalStateException, SystemException;

Source Link

Document

Complete the transaction associated with the current thread.

Usage

From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java

/**
 * Have both cache1 and cache2 do add and commit. cache1 commit should time
 * out since it can't obtain the lock when trying to replicate cache2. On the
 * other hand, cache2 commit will succeed since now that cache1 is rollbacked
 * and lock is released./* w  ww .j  a  va  2 s .  co  m*/
 */
public void testPutTx1() throws Exception {
    final CacheSPI<Object, Object> c1 = this.cache1;

    final Semaphore threadOneFirstPart = new Semaphore(0);
    final Semaphore threadTwoFirstPart = new Semaphore(0);
    final Semaphore threadOneSecondPart = new Semaphore(0);

    Thread t1 = new Thread() {
        public void run() {
            TransactionManager tm;

            try {
                tm = beginTransaction();
                c1.put("/a/b/c", "age", 38);
                c1.put("/a/b/c", "age", 39);
                threadOneFirstPart.release();

                threadTwoFirstPart.acquire();
                try {
                    tm.commit();
                } catch (RollbackException ex) {
                } finally {
                    threadOneSecondPart.release();
                }
            } catch (Throwable ex) {
                ex.printStackTrace();
                t1_ex = ex;
            }
        }
    };

    Thread t2 = new Thread() {
        public void run() {
            TransactionManager tm;

            try {
                threadOneFirstPart.acquire();
                tm = beginTransaction();
                assertNull(cache2.get("/a/b/c", "age"));// must be null as not yet
                                                        // committed
                cache2.put("/a/b/c", "age", 40);

                threadTwoFirstPart.release();

                threadOneSecondPart.acquire();
                assertEquals(40, cache2.get("/a/b/c", "age"));// must not be null
                tm.commit();

                tm = beginTransaction();
                assertEquals("After cache2 commit", 40, cache2.get("/a/b/c", "age"));
                tm.commit();
            } catch (Throwable ex) {
                ex.printStackTrace();
                t2_ex = ex;
            } finally {
                lock.release();
            }
        }
    };

    // Let the game start
    t1.start();
    t2.start();

    t1.join();
    t2.join();

    if (t1_ex != null) {
        fail("Thread1 failed: " + t1_ex);
    }
    if (t2_ex != null) {
        fail("Thread2 failed: " + t2_ex);
    }
}

From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java

/**
 * Test replicated cache with transaction. Idea is to have two threads running
 * a local cache each that is replicating. Depending on whether cache1
 * commit/rollback or not, the cache2.get will get different values. Note that
 * we have used sleep to interpose thread execution sequence. Although it's
 * not fool proof, it is rather simple and intuitive.
 *//*  w  ww  .j  av a  2  s  . co m*/
public void testPutTx() throws Exception {
    TransactionManager tm;

    try {
        cache1.getConfiguration().setSyncCommitPhase(true);
        cache2.getConfiguration().setSyncCommitPhase(true);
        tm = beginTransaction();
        cache1.put("/a/b/c", "age", 38);
        cache1.put("/a/b/c", "age", 39);
        Object val = cache2.get("/a/b/c", "age");// must be null as not yet
                                                 // committed
        assertNull(val);
        tm.commit();

        tm = beginTransaction();
        assertEquals(39, cache2.get("/a/b/c", "age"));// must not be null
        tm.commit();
    } catch (Throwable t) {
        t.printStackTrace();
        t1_ex = t;
    } finally {
        lock.release();
    }
}

From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java

public void testSyncRepl() throws Exception {
    Integer age;//from w  ww  .j  av a 2s .c  om
    Transaction tx;

    cache1.getConfiguration().setSyncCommitPhase(true);
    cache2.getConfiguration().setSyncCommitPhase(true);

    TransactionManager mgr = beginTransaction();
    cache1.put("/a/b/c", "age", 38);
    tx = mgr.suspend();
    assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age"));
    // log.debug("cache1: locks held before commit: "
    //     + CachePrinter.printCacheLockingInfo(cache1));
    // log.debug("cache2: locks held before commit: "
    //     + CachePrinter.printCacheLockingInfo(cache2));
    mgr.resume(tx);
    mgr.commit();
    // log.debug("cache1: locks held after commit: "
    //     + CachePrinter.printCacheLockingInfo(cache1));
    // log.debug("cache2: locks held after commit: "
    //     + CachePrinter.printCacheLockingInfo(cache2));

    // value on cache2 must be 38
    age = (Integer) cache2.get("/a/b/c", "age");
    assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
    assertTrue("\"age\" must be 38", age == 38);
}

From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java

public void testSyncReplWithModficationsOnBothCaches() throws Exception {
    TransactionManager tm;
    final Fqn NODE1 = Fqn.fromString("/one/two/three");
    final Fqn NODE2 = Fqn.fromString("/eins/zwei/drei");

    // create roots first
    cache1.put("/one/two", null);
    cache2.put("/eins/zwei", null);

    cache1.getConfiguration().setSyncCommitPhase(true);
    cache2.getConfiguration().setSyncCommitPhase(true);

    tm = beginTransaction();// w  w  w . ja  va  2  s.c  om
    cache1.put(NODE1, "age", 38);

    cache2.put(NODE2, "age", 39);

    try {
        tm.commit();
        fail("Should not succeed with SERIALIZABLE semantics");
    } catch (Exception e) {
        // should be a classic deadlock here.
    }

    /*
     * assertTrue(cache1.exists(NODE1)); assertTrue(cache1.exists(NODE2));
     * assertTrue(cache1.exists(NODE1)); assertTrue(cache2.exists(NODE2));
     * 
     * age = (Integer) cache1.get(NODE1, "age");
     * assertNotNull("\"age\" obtained from cache1 for " + NODE1 +
     * " must be non-null ", age); assertTrue("\"age\" must be 38", age == 38);
     * 
     * age = (Integer) cache2.get(NODE1, "age");
     * assertNotNull("\"age\" obtained from cache2 for " + NODE1 +
     * " must be non-null ", age); assertTrue("\"age\" must be 38", age == 38);
     * 
     * age = (Integer) cache1.get(NODE2, "age");
     * assertNotNull("\"age\" obtained from cache1 for " + NODE2 +
     * " must be non-null ", age); assertTrue("\"age\" must be 39", age == 39);
     * 
     * age = (Integer) cache2.get(NODE2, "age");
     * assertNotNull("\"age\" obtained from cache2 for " + NODE2 +
     * " must be non-null ", age); assertTrue("\"age\" must be 39", age == 39);
     */

    assertEquals(0, cache1.getNumberOfLocksHeld());
    assertEquals(0, cache2.getNumberOfLocksHeld());
}

From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java

public void testSyncReplWithModficationsOnBothCachesWithRollback() throws Exception {
    TransactionManager tm;
    final Fqn fqn1 = Fqn.fromString("/one/two/three");
    final Fqn fqn2 = Fqn.fromString("/eins/zwei/drei");

    cache1.getConfiguration().setSyncRollbackPhase(true);
    cache2.getConfiguration().setSyncRollbackPhase(true);

    tm = beginTransaction();/* w  w w  . j  av a  2  s . c om*/
    cache1.put(fqn1, "age", 38);
    cache2.put(fqn2, "age", 39);

    // this will rollback the transaction
    Transaction tx = tm.getTransaction();
    tx.registerSynchronization(new TransactionAborter(tx));

    try {
        tm.commit();
        fail("commit should throw a RollbackException, we should not get here");
    } catch (RollbackException rollback) {
    }

    assertEquals(0, cache1.getNumberOfLocksHeld());
    assertEquals(0, cache2.getNumberOfLocksHeld());

    assertEquals(0, cache1.getNumberOfNodes());
    assertEquals(0, cache2.getNumberOfNodes());
}

From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java

/**
 * Test for JBCACHE-361 -- does marking a tx on the remote side rollback-only
 * cause a rollback on the originating side?
 *///from   w  w w .ja v a  2  s . c om
public void testSyncReplWithRemoteRollback() throws Exception {
    TransactionManager tm;
    final Fqn NODE1 = Fqn.fromString("/one/two/three");

    cache1.getConfiguration().setSyncRollbackPhase(true);
    cache2.getConfiguration().setSyncRollbackPhase(true);

    // Test with a rollback on the remote side

    // listener aborts any active tx
    // TransactionAborterListener tal = new TransactionAborterListener(cache2);

    tm = beginTransaction();
    cache1.put(NODE1, "age", 38);

    // instead of a listener lets just get a WL on ROOT on cache2. And hold on
    // to it.
    Transaction tx = tm.suspend();

    tm.begin();
    cache2.getRoot().put("x", "y");
    Transaction tx2 = cache2.getTransactionManager().suspend();

    tm.resume(tx);

    try {
        tm.commit();
        fail("commit should throw a RollbackException, we should not get here");
    } catch (RollbackException rollback) {
    } finally {
        tm.resume(tx2);
        tm.rollback();
    }

    // Sleep, as the commit call to cache2 is async
    Thread.sleep(1000);

    // assertNull(tal.getCallbackException());

    assertEquals(0, cache1.getNumberOfLocksHeld());
    assertEquals(0, cache2.getNumberOfLocksHeld());

    assertEquals(0, cache1.getNumberOfNodes());
    assertEquals(0, cache2.getNumberOfNodes());

}

From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java

public void testPutTxWithRollback() throws Exception {
    final CacheSPI<Object, Object> c2 = this.cache1;
    Thread t1 = new Thread() {
        public void run() {
            TransactionManager tm;

            try {
                lock.acquire();//from www  .j  a  v a 2 s . co  m
                tm = beginTransaction();
                c2.put("/a/b/c", "age", 38);
                c2.put("/a/b/c", "age", 39);
                lock.release();

                Thread.sleep(100);
                lock.acquire();
                tm.rollback();
                lock.release();
            } catch (Throwable ex) {
                ex.printStackTrace();
                t1_ex = ex;
            } finally {
                lock.release();
            }
        }
    };

    Thread t2 = new Thread() {
        public void run() {
            TransactionManager tm;

            try {
                sleep(200);
                Thread.yield();
                lock.acquire();
                tm = beginTransaction();
                assertNull(cache2.get("/a/b/c", "age"));// must be null as not yet
                                                        // committed
                lock.release();

                Thread.sleep(100);
                lock.acquire();
                assertNull(cache2.get("/a/b/c", "age"));// must be null as rolledback
                tm.commit();
                lock.release();
            } catch (Throwable ex) {
                ex.printStackTrace();
                t2_ex = ex;
            } finally {
                lock.release();
            }
        }
    };

    // Let the game start
    t1.start();
    t2.start();

    // Wait for thread to die but put an insurance of 5 seconds on it.
    t1.join();
    t2.join();
    if (t1_ex != null) {
        fail("Thread1 failed: " + t1_ex);
    }
    if (t2_ex != null) {
        fail("Thread2 failed: " + t2_ex);
    }
}

From source file:org.apache.ode.jbi.JbiTestBase.java

private void initOdeDb() throws Exception {
    TransactionManager tm = (TransactionManager) getBean("transactionManager");
    tm.begin();// w w w. j a  va 2 s . com
    Connection conn = ((DataSource) getBean("odeDS")).getConnection();
    Statement s = conn.createStatement();
    s.execute("delete from bpel_process");
    s.close();
    tm.commit();
}

From source file:org.apache.ode.scheduler.simple.SimpleScheduler.java

public <T> T execTransaction(Callable<T> transaction, int timeout) throws Exception, ContextException {
    TransactionManager txm = _txm;
    if (txm == null) {
        throw new ContextException("Cannot locate the transaction manager; the server might be shutting down.");
    }/*from ww w  . ja va  2 s.co m*/

    // The value of the timeout is in seconds. If the value is zero, the transaction service restores the default value.
    if (timeout < 0) {
        throw new IllegalArgumentException("Timeout must be positive, received: " + timeout);
    }

    boolean existingTransaction = false;
    try {
        existingTransaction = txm.getTransaction() != null;
    } catch (Exception ex) {
        String errmsg = "Internal Error, could not get current transaction.";
        throw new ContextException(errmsg, ex);
    }

    // already in transaction, execute and return directly
    if (existingTransaction) {
        return transaction.call();
    }

    // run in new transaction
    Exception ex = null;
    int immediateRetryCount = _immediateTransactionRetryLimit;

    _txm.setTransactionTimeout(timeout);
    if (__log.isDebugEnabled() && timeout != 0)
        __log.debug("Custom transaction timeout: " + timeout);
    try {
        do {
            try {
                if (__log.isDebugEnabled())
                    __log.debug("Beginning a new transaction");
                txm.begin();
            } catch (Exception e) {
                String errmsg = "Internal Error, could not begin transaction.";
                throw new ContextException(errmsg, e);
            }

            try {
                ex = null;
                return transaction.call();
            } catch (Exception e) {
                ex = e;
            } finally {
                if (ex == null) {
                    if (__log.isDebugEnabled())
                        __log.debug("Commiting on " + txm + "...");
                    try {
                        txm.commit();
                    } catch (Exception e2) {
                        ex = e2;
                    }
                } else {
                    if (__log.isDebugEnabled())
                        __log.debug("Rollbacking on " + txm + "...");
                    txm.rollback();
                }

                if (ex != null && immediateRetryCount > 0) {
                    if (__log.isDebugEnabled())
                        __log.debug("Will retry the transaction in " + _immediateTransactionRetryInterval
                                + " msecs on " + _txm + " for error: ", ex);
                    Thread.sleep(_immediateTransactionRetryInterval);
                }
            }
        } while (immediateRetryCount-- > 0);
    } finally {
        // 0 restores the default value
        _txm.setTransactionTimeout(0);
    }

    throw ex;
}

From source file:org.hibernate.search.test.performance.scenario.TestExecutor.java

private void commitTransaction(SessionImplementor session) {
    if (session.getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta()) {
        TransactionManager transactionManager = lookupTransactionManager(session);
        try {//from  ww w.  ja va  2  s . c  o m
            transactionManager.commit();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        session.getTransaction().commit();
    }
}