Search in sources :

Example 11 with LogWriterI18n

use of org.apache.geode.i18n.LogWriterI18n in project geode by apache.

the class JNDIInvoker method doTransactionLookup.

/*
   * Helps in locating TransactionManager in presence of application server scenario. Stores the
   * value of TransactionManager for reference in GemFire system.
   */
private static void doTransactionLookup() throws NamingException {
    Object jndiObject = null;
    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
    for (int i = 0; i < knownJNDIManagers.length; i++) {
        try {
            jndiObject = ctx.lookup(knownJNDIManagers[i][0]);
        } catch (NamingException e) {
            String exception = "JNDIInvoker::doTransactionLookup::Couldn't lookup [" + knownJNDIManagers[i][0] + " (" + knownJNDIManagers[i][1] + ")]";
            if (writer.finerEnabled())
                writer.finer(exception);
        }
        if (jndiObject instanceof TransactionManager) {
            transactionManager = (TransactionManager) jndiObject;
            String exception = "JNDIInvoker::doTransactionLookup::Found TransactionManager for " + knownJNDIManagers[i][1];
            if (writer.fineEnabled())
                writer.fine(exception);
            return;
        } else {
            String exception = "JNDIInvoker::doTransactionLookup::Found TransactionManager of class " + (jndiObject == null ? "null" : jndiObject.getClass()) + " but is not of type javax.transaction.TransactionManager";
            if (writer.fineEnabled())
                writer.fine(exception);
        }
    }
    Class clazz;
    try {
        if (writer.finerEnabled())
            writer.finer("JNDIInvoker::doTransactionLookup::Trying WebSphere 5.1: " + WS_FACTORY_CLASS_5_1);
        clazz = ClassPathLoader.getLatest().forName(WS_FACTORY_CLASS_5_1);
        if (writer.fineEnabled())
            writer.fine("JNDIInvoker::doTransactionLookup::Found WebSphere 5.1: " + WS_FACTORY_CLASS_5_1);
    } catch (ClassNotFoundException ex) {
        try {
            if (writer.finerEnabled())
                writer.finer("JNDIInvoker::doTransactionLookup::Trying WebSphere 5.0: " + WS_FACTORY_CLASS_5_0);
            clazz = ClassPathLoader.getLatest().forName(WS_FACTORY_CLASS_5_0);
            if (writer.fineEnabled())
                writer.fine("JNDIInvoker::doTransactionLookup::Found WebSphere 5.0: " + WS_FACTORY_CLASS_5_0);
        } catch (ClassNotFoundException ex2) {
            try {
                clazz = ClassPathLoader.getLatest().forName(WS_FACTORY_CLASS_4);
                String exception = "JNDIInvoker::doTransactionLookup::Found WebSphere 4: " + WS_FACTORY_CLASS_4;
                if (writer.fineEnabled())
                    writer.fine(exception, ex);
            } catch (ClassNotFoundException ex3) {
                if (writer.finerEnabled())
                    writer.finer("JNDIInvoker::doTransactionLookup::Couldn't find any WebSphere TransactionManager factory class, neither for WebSphere version 5.1 nor 5.0 nor 4");
                throw new NoInitialContextException();
            }
        }
    }
    try {
        Method method = clazz.getMethod("getTransactionManager", (Class[]) null);
        transactionManager = (TransactionManager) method.invoke(null, (Object[]) null);
    } catch (Exception ex) {
        writer.warning(LocalizedStrings.JNDIInvoker_JNDIINVOKER_DOTRANSACTIONLOOKUP_FOUND_WEBSPHERE_TRANSACTIONMANAGER_FACTORY_CLASS_0_BUT_COULDNT_INVOKE_ITS_STATIC_GETTRANSACTIONMANAGER_METHOD, clazz.getName(), ex);
        throw new NameNotFoundException(LocalizedStrings.JNDIInvoker_JNDIINVOKER_DOTRANSACTIONLOOKUP_FOUND_WEBSPHERE_TRANSACTIONMANAGER_FACTORY_CLASS_0_BUT_COULDNT_INVOKE_ITS_STATIC_GETTRANSACTIONMANAGER_METHOD.toLocalizedString(new Object[] { clazz.getName() }));
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) LogWriterI18n(org.apache.geode.i18n.LogWriterI18n) Method(java.lang.reflect.Method) DataSourceCreateException(org.apache.geode.internal.datasource.DataSourceCreateException) SystemException(javax.transaction.SystemException)

Example 12 with LogWriterI18n

use of org.apache.geode.i18n.LogWriterI18n 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 13 with LogWriterI18n

use of org.apache.geode.i18n.LogWriterI18n in project geode by apache.

the class GlobalTransaction method rollback.

/**
   * Delists the XAResources associated with the Global Transaction and Roll back the transaction
   * associated with the current thread.
   * 
   * Concurrency: Some paths invoke this method after taking a lock on "this" while other paths
   * invoke this method without taking a lock on "this". Since both types of path do act on the
   * resourceMap collection, it is being protected by a lock on resourceMap too.
   * 
   * @throws java.lang.SecurityException - Thrown to indicate that the thread is not allowed to roll
   *         back the transaction.
   * @throws java.lang.IllegalStateException - Thrown if the current thread is not associated with a
   *         transaction.
   * @throws SystemException - Thrown if the transaction manager encounters an unexpected error
   *         condition.
   * 
   * @see javax.transaction.TransactionManager#rollback()
   */
public void rollback() throws IllegalStateException, SystemException {
    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
    try {
        XAResource xar = null;
        XAResource xar1 = null;
        int loop = 0;
        synchronized (this.resourceMap) {
            Iterator iterator = resourceMap.entrySet().iterator();
            Boolean isActive = Boolean.FALSE;
            Map.Entry entry;
            while (iterator.hasNext()) {
                try {
                    entry = (Map.Entry) iterator.next();
                    xar = (XAResource) entry.getKey();
                    isActive = (Boolean) entry.getValue();
                    if (loop == 0) {
                        xar1 = xar;
                    }
                    loop++;
                    if (isActive.booleanValue()) {
                        // delistResource(xar, XAResource.TMSUCCESS);
                        xar.end(xid, XAResource.TMSUCCESS);
                        entry.setValue(Boolean.FALSE);
                    }
                } catch (Exception e) {
                    if (VERBOSE)
                        writer.info(LocalizedStrings.ONE_ARG, "GlobalTransaction::rollback:Exception in delisting XAResource", e);
                }
            }
        }
        if (xar1 != null)
            xar1.rollback(xid);
        status = Status.STATUS_ROLLEDBACK;
        if (VERBOSE)
            writer.fine("Transaction rolled back successfully");
    } catch (Exception e) {
        // we will throw an error later, make sure that the synchronizations rollback
        status = Status.STATUS_ROLLEDBACK;
        String exception = LocalizedStrings.GlobalTransaction_GLOBALTRANSACTION_ROLLBACK_ROLLBACK_NOT_SUCCESSFUL_DUE_TO_EXCEPTION_0_1.toLocalizedString(new Object[] { e, " " + (e instanceof XAException ? ("Error Code =" + ((XAException) e).errorCode) : "") });
        if (VERBOSE)
            writer.fine(exception);
        SystemException sysEx = new SystemException(exception);
        sysEx.initCause(e);
        throw sysEx;
    } finally {
        // Map globalTransactions = tm.getGlobalTransactionMap();
        TransactionManagerImpl.getTransactionManager().cleanGlobalTransactionMap(transactions);
        // Asif : Clear the list of transactions
        transactions.clear();
    }
}
Also used : LogWriterI18n(org.apache.geode.i18n.LogWriterI18n) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException)

Example 14 with LogWriterI18n

use of org.apache.geode.i18n.LogWriterI18n in project geode by apache.

the class GlobalTransaction method suspend.

/**
   * suspends the current transaction by deactivating the XAResource (delist)
   */
public void suspend() throws SystemException {
    XAResource xar = null;
    synchronized (this.resourceMap) {
        Iterator iterator = resourceMap.entrySet().iterator();
        Map.Entry entry;
        Boolean isActive = Boolean.FALSE;
        while (iterator.hasNext()) {
            entry = (Map.Entry) iterator.next();
            xar = (XAResource) entry.getKey();
            isActive = (Boolean) entry.getValue();
            if (isActive.booleanValue())
                try {
                    // delistResource(xar, XAResource.TMSUCCESS);
                    xar.end(xid, XAResource.TMSUSPEND);
                    entry.setValue(Boolean.FALSE);
                } catch (Exception e) {
                    String exception = LocalizedStrings.GlobalTransaction_ERROR_WHILE_DELISTING_XARESOURCE_0_1.toLocalizedString(new Object[] { e, " " + (e instanceof XAException ? ("Error Code =" + ((XAException) e).errorCode) : "") });
                    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
                    if (VERBOSE)
                        writer.fine(exception);
                    throw new SystemException(exception);
                }
        /*
         * catch (SystemException e) { String exception =
         * "GlobaTransaction::suspend not succesful due to " + e; LogWriterI18n writer =
         * TransactionUtils.getLogWriter(); if (VERBOSE) writer.fine(exception); throw new
         * SystemException(exception); }
         */
        }
    }
}
Also used : LogWriterI18n(org.apache.geode.i18n.LogWriterI18n) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException)

Example 15 with LogWriterI18n

use of org.apache.geode.i18n.LogWriterI18n in project geode by apache.

the class TransactionImpl method enlistResource.

/**
   * Enlist the XAResource specified to the Global Transaction associated with this transaction.
   * 
   * @param xaRes XAResource to be enlisted
   * @return true, if resource was enlisted successfully, otherwise false.
   * @throws SystemException Thrown if the transaction manager encounters an unexpected error
   *         condition.
   * @throws IllegalStateException Thrown if the transaction in the target object is in the prepared
   *         state or the transaction is inactive.
   * @throws RollbackException Thrown to indicate that the transaction has been marked for rollback
   *         only.
   * 
   * @see javax.transaction.Transaction#enlistResource(javax.transaction.xa.XAResource)
   */
public boolean enlistResource(XAResource xaRes) throws RollbackException, IllegalStateException, SystemException {
    gtx = tm.getGlobalTransaction();
    if (gtx == null) {
        String exception = LocalizedStrings.TransactionImpl_TRANSACTIONIMPL_ENLISTRESOURCE_NO_GLOBAL_TRANSACTION_EXISTS.toLocalizedString();
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (writer.fineEnabled())
            writer.fine(exception);
        throw new SystemException(exception);
    }
    return gtx.enlistResource(xaRes);
}
Also used : LogWriterI18n(org.apache.geode.i18n.LogWriterI18n)

Aggregations

LogWriterI18n (org.apache.geode.i18n.LogWriterI18n)38 SystemException (javax.transaction.SystemException)11 DistributedSystemDisconnectedException (org.apache.geode.distributed.DistributedSystemDisconnectedException)8 HeuristicMixedException (javax.transaction.HeuristicMixedException)4 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)4 InvalidTransactionException (javax.transaction.InvalidTransactionException)4 NotSupportedException (javax.transaction.NotSupportedException)4 RollbackException (javax.transaction.RollbackException)4 CancelException (org.apache.geode.CancelException)4 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)4 Transaction (javax.transaction.Transaction)3 Region (org.apache.geode.cache.Region)3 Test (org.junit.Test)3 Iterator (java.util.Iterator)2 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)2 CacheWriterException (org.apache.geode.cache.CacheWriterException)2 CommitConflictException (org.apache.geode.cache.CommitConflictException)2 CommitIncompleteException (org.apache.geode.cache.CommitIncompleteException)2 Struct (org.apache.geode.cache.query.Struct)2 ClientHealthStats (org.apache.geode.internal.admin.remote.ClientHealthStats)2