Example usage for javax.transaction TransactionManager begin

List of usage examples for javax.transaction TransactionManager begin

Introduction

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

Prototype

public void begin() throws NotSupportedException, SystemException;

Source Link

Document

Create a new transaction and associate it with the current thread.

Usage

From source file:org.apache.servicemix.jms.JmsSpringJcaTest.java

public void testInOnlySyncWithAsyncConsumer() throws Exception {
    TransactionManager tm = (TransactionManager) getBean("transactionManager");
    tm.begin();
    Destination dest = client.createDestination("endpoint:http://test/MyProviderService/async");
    InOnly me = dest.createInOnlyExchange();
    me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>"));
    client.sendSync(me);//from  w  w  w.  j  av a2s . c  om
    assertEquals(ExchangeStatus.DONE, me.getStatus());
    tm.commit();
    receiver.getMessageList().assertMessagesReceived(1);
}

From source file:org.apache.servicemix.jms.JmsSpringJcaTest.java

public void testInOnlySyncWithSyncConsumer() throws Exception {
    TransactionManager tm = (TransactionManager) getBean("transactionManager");
    tm.begin();
    Destination dest = client.createDestination("endpoint:http://test/MyProviderService/synchronous");
    InOnly me = dest.createInOnlyExchange();
    me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>"));
    client.sendSync(me);/*from www.j av a2  s. c  om*/
    assertEquals(ExchangeStatus.DONE, me.getStatus());
    tm.commit();
    receiver.getMessageList().assertMessagesReceived(1);
}

From source file:org.apache.servicemix.jms.JmsSpringJcaTest.java

public void testInOnlyWithAsyncConsumer() throws Exception {
    TransactionManager tm = (TransactionManager) getBean("transactionManager");
    tm.begin();
    Destination dest = client.createDestination("endpoint:http://test/MyProviderService/async");
    InOnly me = dest.createInOnlyExchange();
    me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>"));
    client.send(me);//  w  w  w  .  ja va 2  s. com
    tm.commit();
    me = (InOnly) client.receive();
    assertEquals(ExchangeStatus.DONE, me.getStatus());
    receiver.getMessageList().assertMessagesReceived(1);
}

From source file:org.apache.servicemix.jms.JmsSpringJcaTest.java

public void testInOnlyWithSyncConsumer() throws Exception {
    TransactionManager tm = (TransactionManager) getBean("transactionManager");
    tm.begin();
    Destination dest = client.createDestination("endpoint:http://test/MyProviderService/synchronous");
    InOnly me = dest.createInOnlyExchange();
    me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>"));
    client.send(me);/*w w  w . j  a  va 2s . co  m*/
    tm.commit();
    me = (InOnly) client.receive();
    assertEquals(ExchangeStatus.DONE, me.getStatus());
    receiver.getMessageList().assertMessagesReceived(1);
}

From source file:org.apache.servicemix.jms.JmsSpringJcaTest.java

public void testInOut() throws Exception {
    TransactionManager tm = (TransactionManager) getBean("transactionManager");
    tm.begin();
    InOut me = client.createInOutExchange();
    me.setService(new QName("http://test", "MyProviderService"));
    me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>"));
    client.send(me);//  ww w  .j  av a 2s. c  om
    tm.commit();
    me = (InOut) client.receive();
    assertEquals(ExchangeStatus.ERROR, me.getStatus());
    assertNotNull(me.getError());
    assertTrue(me.getError() instanceof UnsupportedOperationException);
}

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

private TransactionManager beginTransaction(CacheSPI c) throws SystemException, NotSupportedException {
    TransactionManager mgr = c.getConfiguration().getRuntimeConfig().getTransactionManager();
    mgr.begin();
    return mgr;//from   w  w w . j  a v a  2 s.c om
}

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. j  ava  2 s .  c  o m*/
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:org.apache.ode.jbi.JbiTestBase.java

private void initOdeDb() throws Exception {
    TransactionManager tm = (TransactionManager) getBean("transactionManager");
    tm.begin();
    Connection conn = ((DataSource) getBean("odeDS")).getConnection();
    Statement s = conn.createStatement();
    s.execute("delete from bpel_process");
    s.close();/*from   ww w .j av a2 s.c  om*/
    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  w  w  w.jav a 2 s  .  c om

    // 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.apache.openjpa.kernel.AbstractBrokerFactory.java

/**
 * Synchronize the given broker with a managed transaction,
 * optionally starting one if none is in progress.
 *
 * @return true if synched with transaction, false otherwise
 *//* w  w w  .j a v  a 2 s  .  c o  m*/
boolean syncWithManagedTransaction(BrokerImpl broker, boolean begin) {
    Transaction trans;
    try {
        ManagedRuntime mr = broker.getManagedRuntime();
        TransactionManager tm = mr.getTransactionManager();
        if (tm == null) {
            throw new InternalException(_loc.get("null-transactionmanager", mr));
        }
        trans = tm.getTransaction();
        if (trans != null && (trans.getStatus() == Status.STATUS_NO_TRANSACTION
                || trans.getStatus() == Status.STATUS_UNKNOWN))
            trans = null;

        if (trans == null && begin) {
            tm.begin();
            trans = tm.getTransaction();
        } else if (trans == null)
            return false;

        // synch broker and trans
        trans.registerSynchronization(broker);

        // we don't need to synchronize on brokers or guard against multiple
        // threads using the same trans since one JTA transaction can never
        // be active on multiple concurrent threads.
        Object txKey = mr.getTransactionKey();
        Collection<Broker> brokers = _transactional.get(txKey);

        if (brokers == null) {
            brokers = new ArrayList<Broker>(2);
            _transactional.put(txKey, brokers);
            trans.registerSynchronization(new RemoveTransactionSync(txKey));
        }
        brokers.add(broker);

        return true;
    } catch (OpenJPAException ke) {
        throw ke;
    } catch (Exception e) {
        throw new GeneralException(e);
    }
}