Search in sources :

Example 36 with SystemException

use of javax.transaction.SystemException in project geode by apache.

the class JNDIInvoker method mapTransactions.

/**
   * Bind the transaction resources. Bind UserTransaction and TransactionManager.
   * <p>
   * If there is pre-existing JNDI tree in the system and TransactionManager / UserTransaction is
   * already bound, GemFire will make use of these resources, but if TransactionManager /
   * UserTransaction is not available, the GemFire TransactionManager / UserTransaction will be
   * bound to the JNDI tree.
   * </p>
   * 
   */
public static void mapTransactions(DistributedSystem distSystem) {
    try {
        TransactionUtils.setLogWriter(distSystem.getLogWriter().convertToLogWriterI18n());
        cleanup();
        if (IGNORE_JTA) {
            return;
        }
        ctx = new InitialContext();
        doTransactionLookup();
    } catch (NamingException ne) {
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (ne instanceof NoInitialContextException) {
            String exception = "JNDIInvoker::mapTransactions:: No application server context found, Starting GemFire JNDI Context Context ";
            if (writer.finerEnabled())
                writer.finer(exception);
            try {
                initializeGemFireContext();
                transactionManager = TransactionManagerImpl.getTransactionManager();
                ctx.rebind("java:/TransactionManager", transactionManager);
                if (writer.fineEnabled())
                    writer.fine("JNDIInvoker::mapTransactions::Bound TransactionManager to Context GemFire JNDI Tree");
                UserTransactionImpl utx = new UserTransactionImpl();
                ctx.rebind("java:/UserTransaction", utx);
                if (writer.fineEnabled())
                    writer.fine("JNDIInvoker::mapTransactions::Bound Transaction to Context GemFire JNDI Tree");
            } catch (NamingException ne1) {
                if (writer.infoEnabled())
                    writer.info(LocalizedStrings.JNDIInvoker_JNDIINVOKERMAPTRANSACTIONSNAMINGEXCEPTION_WHILE_BINDING_TRANSACTIONMANAGERUSERTRANSACTION_TO_GEMFIRE_JNDI_TREE);
            } catch (SystemException se1) {
                if (writer.infoEnabled())
                    writer.info(LocalizedStrings.JNDIInvoker_JNDIINVOKERMAPTRANSACTIONSSYSTEMEXCEPTION_WHILE_BINDING_USERTRANSACTION_TO_GEMFIRE_JNDI_TREE);
            }
        } else if (ne instanceof NameNotFoundException) {
            String exception = "JNDIInvoker::mapTransactions:: No TransactionManager associated to Application server context, trying to bind GemFire TransactionManager";
            if (writer.finerEnabled())
                writer.finer(exception);
            try {
                transactionManager = TransactionManagerImpl.getTransactionManager();
                ctx.rebind("java:/TransactionManager", transactionManager);
                if (writer.fineEnabled())
                    writer.fine("JNDIInvoker::mapTransactions::Bound TransactionManager to Application Server Context");
                UserTransactionImpl utx = new UserTransactionImpl();
                ctx.rebind("java:/UserTransaction", utx);
                if (writer.fineEnabled())
                    writer.fine("JNDIInvoker::mapTransactions::Bound UserTransaction to Application Server Context");
            } catch (NamingException ne1) {
                if (writer.infoEnabled())
                    writer.info(LocalizedStrings.JNDIInvoker_JNDIINVOKERMAPTRANSACTIONSNAMINGEXCEPTION_WHILE_BINDING_TRANSACTIONMANAGERUSERTRANSACTION_TO_APPLICATION_SERVER_JNDI_TREE);
            } catch (SystemException se1) {
                if (writer.infoEnabled())
                    writer.info(LocalizedStrings.JNDIInvoker_JNDIINVOKERMAPTRANSACTIONSSYSTEMEXCEPTION_WHILE_BINDING_TRANSACTIONMANAGERUSERTRANSACTION_TO_APPLICATION_SERVER_JNDI_TREE);
            }
        }
    }
}
Also used : SystemException(javax.transaction.SystemException) UserTransactionImpl(org.apache.geode.internal.jta.UserTransactionImpl) LogWriterI18n(org.apache.geode.i18n.LogWriterI18n)

Example 37 with SystemException

use of javax.transaction.SystemException in project geode by apache.

the class ExceptionJUnitTest method testAddNullTransaction.

@Test
public void testAddNullTransaction() throws Exception {
    try {
        utx.begin();
        GlobalTransaction gtx = tm.getGlobalTransaction();
        Transaction txn = null;
        gtx.addTransaction(txn);
        utx.commit();
        fail("SystemException not thrown on adding null transaction");
    } catch (SystemException e) {
        utx.commit();
    }
}
Also used : Transaction(javax.transaction.Transaction) UserTransaction(javax.transaction.UserTransaction) SystemException(javax.transaction.SystemException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 38 with SystemException

use of javax.transaction.SystemException in project geode by apache.

the class TxnTimeOutDUnitTest method runTest3.

public static void runTest3(Object o) throws SystemException, NotSupportedException, NamingException, InterruptedException {
    boolean exceptionOccurred = false;
    int sleeptime = ((Integer) o).intValue();
    Context ctx = cache.getJNDIContext();
    UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
    utx.begin();
    utx.setTransactionTimeout(sleeptime);
    Thread.sleep(sleeptime * 2000);
    try {
        utx.commit();
    } catch (Exception e) {
        exceptionOccurred = true;
    }
    if (!exceptionOccurred) {
        fail("exception did not occur although was supposed to occur");
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) NamingException(javax.naming.NamingException) IOException(java.io.IOException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException)

Example 39 with SystemException

use of javax.transaction.SystemException in project jackrabbit by apache.

the class UserTransactionImpl method rollback.

/**
     * @see javax.transaction.UserTransaction#rollback
     */
public void rollback() throws IllegalStateException, SecurityException, SystemException {
    if (status != Status.STATUS_ACTIVE && status != Status.STATUS_MARKED_ROLLBACK) {
        throw new IllegalStateException("Transaction not active");
    }
    try {
        for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
            XAResource resource = (XAResource) it.next();
            XidImpl xid = (XidImpl) xaResources.get(resource);
            resource.end(xid, XAResource.TMFAIL);
        }
        status = Status.STATUS_ROLLING_BACK;
        for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
            XAResource resource = (XAResource) it.next();
            XidImpl xid = (XidImpl) xaResources.get(resource);
            resource.rollback(xid);
        }
        status = Status.STATUS_ROLLEDBACK;
    } catch (XAException e) {
        SystemException se = new SystemException("Unable to rollback transaction: XA_ERR=" + e.errorCode);
        se.initCause(e.getCause());
        throw se;
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) Iterator(java.util.Iterator)

Example 40 with SystemException

use of javax.transaction.SystemException in project jackrabbit by apache.

the class UserTransactionImpl method commit.

/**
     * @see javax.transaction.UserTransaction#commit
     */
public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, RollbackException, SecurityException, SystemException {
    if (status != Status.STATUS_ACTIVE) {
        throw new IllegalStateException("Transaction not active");
    }
    try {
        for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
            XAResource resource = (XAResource) it.next();
            XidImpl xid = (XidImpl) xaResources.get(resource);
            resource.end(xid, XAResource.TMSUCCESS);
        }
        status = Status.STATUS_PREPARING;
        for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
            XAResource resource = (XAResource) it.next();
            XidImpl xid = (XidImpl) xaResources.get(resource);
            resource.prepare(xid);
        }
        status = Status.STATUS_PREPARED;
        status = Status.STATUS_COMMITTING;
        if (distributedThreadAccess) {
            Thread distributedThread = new Thread() {

                public void run() {
                    try {
                        for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
                            XAResource resource = (XAResource) it.next();
                            XidImpl xid = (XidImpl) xaResources.get(resource);
                            resource.commit(xid, false);
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }
                }
            };
            distributedThread.start();
            distributedThread.join(1000);
            if (distributedThread.isAlive()) {
                throw new SystemException("Commit from different thread but same XID must not block");
            }
        } else {
            for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
                XAResource resource = (XAResource) it.next();
                XidImpl xid = (XidImpl) xaResources.get(resource);
                resource.commit(xid, false);
            }
        }
        status = Status.STATUS_COMMITTED;
    } catch (XAException e) {
        if (e.errorCode >= XAException.XA_RBBASE && e.errorCode <= XAException.XA_RBEND) {
            RollbackException re = new RollbackException("Transaction rolled back: XA_ERR=" + e.errorCode);
            re.initCause(e.getCause());
            throw re;
        } else {
            SystemException se = new SystemException("Unable to commit transaction: XA_ERR=" + e.errorCode);
            se.initCause(e.getCause());
            throw se;
        }
    } catch (InterruptedException e) {
        throw new SystemException("Thread.join() interrupted");
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) Iterator(java.util.Iterator) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) XAException(javax.transaction.xa.XAException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Aggregations

SystemException (javax.transaction.SystemException)102 Transaction (javax.transaction.Transaction)34 RollbackException (javax.transaction.RollbackException)29 NotSupportedException (javax.transaction.NotSupportedException)22 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)18 IOException (java.io.IOException)16 HeuristicMixedException (javax.transaction.HeuristicMixedException)16 UserTransaction (javax.transaction.UserTransaction)14 XAException (javax.transaction.xa.XAException)13 Test (org.junit.Test)12 TransactionManager (javax.transaction.TransactionManager)11 SQLException (java.sql.SQLException)10 LogWriterI18n (org.apache.geode.i18n.LogWriterI18n)10 InvalidTransactionException (javax.transaction.InvalidTransactionException)9 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)8 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)8 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 XAResource (javax.transaction.xa.XAResource)7 Synchronization (javax.transaction.Synchronization)6 File (java.io.File)5