Example usage for javax.transaction Transaction enlistResource

List of usage examples for javax.transaction Transaction enlistResource

Introduction

In this page you can find the example usage for javax.transaction Transaction enlistResource.

Prototype

public boolean enlistResource(XAResource xaRes)
        throws RollbackException, IllegalStateException, SystemException;

Source Link

Document

Enlist the resource specified with the transaction associated with the target Transaction object.

Usage

From source file:org.jboss.narayana.quickstart.spring.service.ExampleService.java

@Transactional
public void testCommit() throws Exception {
    try {/*from   www  .java  2 s . com*/
        System.out.println("testCommit");
        Transaction transaction = tm.getTransaction();
        transaction.enlistResource(xaResource);
        jdbc.execute("insert into example values (1, 'test1')");
        System.out.println("testCommit OK");
    } catch (Exception e) {
        System.out.println("testCommit FAIL with " + e);
        throw e;
    }
}

From source file:org.jboss.narayana.quickstart.spring.service.ExampleService.java

@Transactional
public void testRecovery() {
    try {/*from  ww w . j a  v  a2  s  .c  o  m*/
        System.out.println("testRecovery");
        Transaction transaction = tm.getTransaction();
        xaResource.setFault(DummyXAResource.faultType.HALT);
        transaction.enlistResource(xaResource);
        jdbc.execute("insert into example values (1, 'test1')");
    } catch (Exception e) {
        System.out.println("testRecovery FAIL with " + e);
    }
}

From source file:io.strandberg.xadisk.XADiskSessionFactory.java

public XASession getXASession() throws SystemException, RollbackException {

    final Transaction transaction = transactionManager.getTransaction();

    synchronized (transaction) {

        // get the xaSession associated with the current transaction - if any
        XASession xaSession = xaSessionMap.get(transaction);

        if (xaSession == null) {

            // create a new xaSession
            xaSession = xaFileSystem.createSessionForXATransaction();
            logger.debug("New XASession has been created");

            // enlist the xaSessions XAResource
            transaction.enlistResource(xaSession.getXAResource());
            logger.debug("XASession's XAResource has been enlisted in transaction");

            // Remove the xaSession from the xaSessionMap after the transaction is completed
            transaction.registerSynchronization(new Synchronization() {
                public void beforeCompletion() {
                }/*from w  w  w . j a  v a2s.c o m*/

                public void afterCompletion(int status) {
                    xaSessionMap.remove(transaction);
                    logger.debug("XASession has been removed from the XASession Map");
                }
            });

            // Associate the transaction with the xaSession
            xaSessionMap.put(transaction, xaSession);
            logger.debug("New XASession added to XASession Map");

        } else {
            logger.debug("XASession found in XASession Map");
        }

        return xaSession;
    }
}

From source file:org.apache.ofbiz.entity.transaction.TransactionUtil.java

public static void enlistResource(XAResource resource) throws GenericTransactionException {
    if (resource == null) {
        return;// w w  w . j ava  2  s .c  om
    }

    try {
        TransactionManager tm = TransactionFactoryLoader.getInstance().getTransactionManager();
        if (tm != null && tm.getStatus() == STATUS_ACTIVE) {
            Transaction tx = tm.getTransaction();
            if (tx != null) {
                tx.enlistResource(resource);
            }
        }
    } catch (RollbackException e) {
        //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();
        throw new GenericTransactionException(
                "Roll Back error, could not enlist resource in transaction even though transactions are available, current transaction rolled back",
                e);
    } catch (SystemException e) {
        //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();
        throw new GenericTransactionException(
                "System error, could not enlist resource in transaction even though transactions are available",
                e);
    }
}

From source file:org.apache.synapse.commons.transaction.TranscationManger.java

public static void bindConnection(final Connection conn) throws Exception {
    long key = Thread.currentThread().getId();
    try {//from   w  w  w.  j ava 2 s.  co  m
        if (conn instanceof XAConnection) {
            Transaction tx = transactions.get().get(key);
            XAResource xaRes = ((XAConnection) conn).getXAResource();

            if (!isXAResourceEnlisted(xaRes)) {
                tx.enlistResource(xaRes);
                addToEnlistedXADataSources(xaRes, key);
                log.debug(" DS enlisted in thread " + key + " XA Resource : " + xaRes.hashCode());
            }
        }

    } catch (Exception ex) {
        StringBuilder logMsg = new StringBuilder();
        Connection actual = ((javax.sql.PooledConnection) conn).getConnection();
        logMsg.append(" Thread Id : " + key)
                .append(" BIND ERROR , Transaction Manager status : " + txManagers.get().get(key).getStatus())
                .append("\n")
                .append(" BIND ERROR , Transaction status : " + transactions.get().get(key).getStatus())
                .append("\n").append(" JDBC Connection status : " + actual.isClosed()).append("\n")
                .append(" BIND ERROR  : " + ex);
        log.error(logMsg.toString());
        rollbackTransaction(true, key);
        throw ex;
    }
}

From source file:org.compass.core.transaction.XATransaction.java

protected void doBindToTransaction(Transaction tx, InternalCompassSession session, boolean newTransaction)
        throws Exception {
    tx.enlistResource(new CompassXAResource(session));
}

From source file:org.csc.phynixx.xa.PhynixxManagedXAConnection.java

/**
 * if necessary the current xa resource is enlisted in the current TX.
 * <p/>//w  w  w  . j  ava 2s.co  m
 * The enlistment calls the
 * {@link javax.transaction.xa.XAResource#start(javax.transaction.xa.Xid, int)}
 * . This call associates the Xid with the current instance
 */
private void enlistTransaction() {

    this.cleanupTransactionBinding();

    TransactionBindingType transactionBindingType = this.transactionBinding.getTransactionBindingType();

    // not associated to global transaction, try to associate a global
    // transaction
    if (this.isInGlobalTransaction() && transactionBindingType != TransactionBindingType.GlobalTransaction) {
        try {
            Transaction ntx = this.transactionManager.getTransaction();

            // Bitronix calls start on reaction of enlist --- check if cycle
            if (!enlisted && ntx != null) {
                this.enlisted = true;
                // enlisted makes startTransaactionalBranch calling
                this.enlisted = ntx.enlistResource(this.xaResource);
                if (!enlisted) {
                    LOG.error("Enlisting " + xaResource + " failed");
                } else {

                }
            } else {
                LOG.debug("SampleXAConnection:connectionRequiresTransaction (no globalTransaction found)");
            }
        } catch (RollbackException n) {
            LOG.error("SampleXAConnection:prevokeAction enlistResource exception : " + n.toString());
        } catch (SystemException n) {
            LOG.error("SampleXAConnection:connectionRequiresTransaction " + n + "\n"
                    + ExceptionUtils.getStackTrace(n));
            throw new DelegatedRuntimeException(n);
        }
    } else if (transactionBindingType == TransactionBindingType.NoTransaction) {
        this.transactionBinding.activateLocalTransaction(
                new LocalTransactionProxy<C>(this.managedConnectionFactory.getManagedConnection()));
    } else { // In Global Transaction and associated to a global transaction
             // => nothing to do
        if (this.isInGlobalTransaction()
                && transactionBindingType == TransactionBindingType.GlobalTransaction) {
            // Not in Global Transaction and associated to a local
            // transaction => nothing to do
        } else if (!this.isInGlobalTransaction()
                && transactionBindingType == TransactionBindingType.LocalTransaction) {
        }
    }

}

From source file:org.etk.entity.engine.plugins.transaction.TransactionUtil.java

public static void enlistResource(XAResource resource) throws GenericTransactionException {
    if (resource == null) {
        return;/* w  w w .  j av  a 2 s  .  c o m*/
    }

    try {
        TransactionManager tm = TransactionFactory.getTransactionManager();
        if (tm != null && tm.getStatus() == STATUS_ACTIVE) {
            Transaction tx = tm.getTransaction();
            if (tx != null) {
                tx.enlistResource(resource);
            }
        }
    } catch (RollbackException e) {
        // This is Java 1.4 only, but useful for certain debuggins: Throwable t =
        // e.getCause() == null ? e : e.getCause();
        throw new GenericTransactionException(
                "Roll Back error, could not enlist resource in transaction even though transactions are available, current transaction rolled back",
                e);
    } catch (SystemException e) {
        // This is Java 1.4 only, but useful for certain debuggins: Throwable t =
        // e.getCause() == null ? e : e.getCause();
        throw new GenericTransactionException(
                "System error, could not enlist resource in transaction even though transactions are available",
                e);
    }
}

From source file:org.jboss.jbossts.fileio.xalib.txdirs.dir.XADir.java

/**
 * This method must be used after a <code>TransactionManager</code>
 * has begun and within the boundaries of a transaction (<code>begin,
 * commit/rollback</code>).//from   www .ja  v  a  2  s  . c  om
 * <p>
 * The method also creates a new {@link XAFileResourceManager} and
 * enlists it to the transaction obtained by the <code>txnMngr</code>.
 *
 * @param txnMngr the <code>TransactionManager</code> used to commit
 *                or rollback the file operations
 * @exception javax.transaction.RollbackException
 *            if an error occurs while enlisting the <code>XAResource</code>
 * @exception javax.transaction.SystemException
 *            if there is a problem enlisting the XAResource created
 *            within this method or when the <code>getTransaction</code> in the
 *            <code>TransactionManager</code> fails or when the TransactionManager
 *            is not in ready (<code>Status.ACTIVE</code>) mode
 */
public synchronized void startTransactionOn(TransactionManager txnMngr)
        throws SystemException, RollbackException {
    curTxId = "txDir-" + freMngr.getWorkDir().replace('/', '_').replace('\\', '_').replace(':', '_') + "_"
            + Thread.currentThread().getId() + "!" + System.nanoTime();
    XAFileResourceManager xafre = new XAFileResourceManager(freMngr, curTxId);
    Transaction tx = txnMngr.getTransaction();
    tx.enlistResource(xafre);
}

From source file:org.mule.test.integration.transaction.XAResourceManagerTestCase.java

@Test
public void testTxBehaviour() throws Exception {
    TestXAResourceManager rm = new TestXAResourceManager();
    rm.start();/*from   w w  w  .ja va2  s.c o  m*/
    DefaultXASession s = rm.createSession();

    tm.begin();
    Transaction tx = tm.getTransaction();
    tx.enlistResource(s);

    tx.delistResource(s, XAResource.TMSUCCESS);
    tx.commit();
}