Search in sources :

Example 6 with LogWriterI18n

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

the class TransactionManagerImpl method commit.

/**
   * Complete the transaction associated with the current thread by calling the
   * GlobalTransaction.commit(). When this method completes, the thread is no longer associated with
   * a transaction.
   * 
   * @throws RollbackException - Thrown to indicate that the transaction has been rolled back rather
   *         than committed.
   * @throws HeuristicMixedException - Thrown to indicate that a heuristic decision was made and
   *         that some relevant updates have been committed while others have been rolled back.
   * @throws HeuristicRollbackException - Thrown to indicate that a heuristic decision was made and
   *         that all relevant updates have been rolled back.
   * @throws java.lang.SecurityException - Thrown to indicate that the thread is not allowed to
   *         commit 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#commit()
   */
public void commit() throws HeuristicRollbackException, RollbackException, HeuristicMixedException, SystemException {
    if (!isActive) {
        throw new SystemException(LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGER_INVALID.toLocalizedString());
    }
    int cozOfException = -1;
    Transaction transactionImpl = getTransaction();
    if (transactionImpl == null) {
        String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_IS_NULL_CANNOT_COMMIT_A_NULL_TRANSACTION.toLocalizedString();
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (VERBOSE)
            writer.fine(exception);
        throw new IllegalStateException(exception);
    }
    GlobalTransaction gtx = getGlobalTransaction(transactionImpl);
    if (gtx == null) {
        String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_GLOBAL_TRANSACTION_IS_NULL_CANNOT_COMMIT_A_NULL_GLOBAL_TRANSACTION.toLocalizedString();
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (VERBOSE)
            writer.fine(exception);
        throw new SystemException(exception);
    }
    boolean isCommit = false;
    // ensure only one thread can commit. Use a synchronized block
    // Asif
    int status = -1;
    if (((status = gtx.getStatus()) == Status.STATUS_ACTIVE) || status == Status.STATUS_MARKED_ROLLBACK) {
        synchronized (gtx) {
            if ((status = gtx.getStatus()) == Status.STATUS_ACTIVE) {
                gtx.setStatus(Status.STATUS_COMMITTING);
                isCommit = true;
            } else if (status == Status.STATUS_MARKED_ROLLBACK) {
                gtx.setStatus(Status.STATUS_ROLLING_BACK);
                cozOfException = MARKED_ROLLBACK;
            } else {
                String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_NOT_ACTIVE_CANNOT_BE_COMMITTED_TRANSACTION_STATUS_0.toLocalizedString(Integer.valueOf(status));
                LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
                if (VERBOSE)
                    writer.fine(exception);
                throw new IllegalStateException(exception);
            }
        }
    } else {
        String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_IS_NOT_ACTIVE_AND_CANNOT_BE_COMMITTED.toLocalizedString();
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (VERBOSE)
            writer.fine(exception);
        throw new IllegalStateException(exception);
    }
    // Only one thread can call commit (the first thread to do reach the block
    // above).
    // Before commiting the notifications to be done before the done are called
    // the global transaction is called and then the after completion
    // notifications
    // are taken care of. The transactions associated to the global
    // transactions are
    // removed from the map and also the tread to transaction.
    //
    // Asif : Store the thrown Exception in case of commit .
    // Reuse it for thrwing later.
    // Asif TODO:Verify if it is a good practise
    boolean isClean = false;
    Exception e = null;
    try {
        ((TransactionImpl) transactionImpl).notifyBeforeCompletion();
        isClean = true;
    } catch (Exception ge) {
        // Asif : Just mark the Tranxn to setRollbackOnly to ensure Rollback
        setRollbackOnly();
        cozOfException = EXCEPTION_IN_NOTIFY_BEFORE_COMPLETION;
        e = ge;
    }
    // will be harmless
    if (isCommit) {
        synchronized (gtx) {
            if ((status = gtx.getStatus()) == Status.STATUS_COMMITTING) {
                // and appropriately mark the exception code
                try {
                    gtx.commit();
                } catch (RollbackException rbe) {
                    e = rbe;
                    cozOfException = COMMIT_FAILED_SO_ROLLEDBAK;
                } catch (SystemException se) {
                    e = se;
                    cozOfException = COMMIT_FAILED_ROLLBAK_ALSO_FAILED;
                }
            } else if (status == Status.STATUS_ROLLING_BACK) {
                try {
                    gtx.rollback();
                    if (isClean)
                        cozOfException = MARKED_ROLLBACK;
                } catch (SystemException se) {
                    e = se;
                    cozOfException = ROLLBAK_FAILED;
                }
            }
        }
    } else {
        try {
            gtx.rollback();
        } catch (SystemException se) {
            e = se;
            cozOfException = ROLLBAK_FAILED;
        }
    }
    try {
        ((TransactionImpl) transactionImpl).notifyAfterCompletion(status = gtx.getStatus());
    } catch (Exception ge) {
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (writer.infoEnabled())
            writer.info(LocalizedStrings.TransactionManagerImpl_EXCEPTION_IN_NOTIFY_AFTER_COMPLETION_DUE_TO__0, ge.getMessage(), ge);
    }
    Thread thread = Thread.currentThread();
    transactionMap.remove(thread);
    this.gtxSet.remove(gtx);
    if (status != Status.STATUS_COMMITTED) {
        switch(cozOfException) {
            case EXCEPTION_IN_NOTIFY_BEFORE_COMPLETION:
                {
                    String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_ROLLED_BACK_BECAUSE_OF_EXCEPTION_IN_NOTIFYBEFORECOMPLETION_FUNCTION_CALL_ACTUAL_EXCEPTION_0.toLocalizedString();
                    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
                    if (VERBOSE)
                        writer.fine(exception, e);
                    RollbackException re = new RollbackException(exception);
                    re.initCause(e);
                    throw re;
                }
            case MARKED_ROLLBACK:
                {
                    String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_ROLLED_BACK_BECAUSE_A_USER_MARKED_IT_FOR_ROLLBACK.toLocalizedString();
                    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
                    if (VERBOSE)
                        writer.fine(exception, e);
                    throw new RollbackException(exception);
                }
            case COMMIT_FAILED_SO_ROLLEDBAK:
                {
                    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
                    if (VERBOSE)
                        writer.fine(e);
                    throw (RollbackException) e;
                }
            case COMMIT_FAILED_ROLLBAK_ALSO_FAILED:
            case ROLLBAK_FAILED:
                {
                    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
                    if (VERBOSE)
                        writer.fine(e);
                    throw (SystemException) e;
                }
        }
    }
    gtx.setStatus(Status.STATUS_NO_TRANSACTION);
}
Also used : SystemException(javax.transaction.SystemException) Transaction(javax.transaction.Transaction) LogWriterI18n(org.apache.geode.i18n.LogWriterI18n) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) InvalidTransactionException(javax.transaction.InvalidTransactionException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) CancelException(org.apache.geode.CancelException)

Example 7 with LogWriterI18n

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

the class TransactionManagerImpl method setRollbackOnly.

/**
   * Set the Global Transaction status (Associated with the current thread) to be RollBackOnly
   * 
   * Becauce we are using one phase commit, we are not considering Prepared and preparing states.
   * 
   * @see javax.transaction.TransactionManager#setRollbackOnly()
   */
public void setRollbackOnly() throws IllegalStateException, SystemException {
    if (!isActive) {
        throw new SystemException(LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGER_INVALID.toLocalizedString());
    }
    GlobalTransaction gtx = getGlobalTransaction();
    if (gtx == null) {
        String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_SETROLLBACKONLY_NO_GLOBAL_TRANSACTION_EXISTS.toLocalizedString();
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (VERBOSE)
            writer.fine(exception);
        throw new SystemException(exception);
    }
    synchronized (gtx) {
        int status = gtx.getStatus();
        if (status == Status.STATUS_ACTIVE)
            gtx.setRollbackOnly();
        else if (status == Status.STATUS_COMMITTING)
            gtx.setStatus(Status.STATUS_ROLLING_BACK);
        else if (status == Status.STATUS_ROLLING_BACK)
            // Dont do anything
            ;
        else {
            String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_SETROLLBACKONLY_TRANSACTION_CANNOT_BE_MARKED_FOR_ROLLBACK_TRANSCATION_STATUS_0.toLocalizedString(Integer.valueOf(status));
            LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
            if (VERBOSE)
                writer.fine(exception);
            throw new IllegalStateException(exception);
        }
    }
    // Asif : Log after exiting synch block
    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
    if (VERBOSE)
        writer.fine("Transaction Set to Rollback only");
}
Also used : SystemException(javax.transaction.SystemException) LogWriterI18n(org.apache.geode.i18n.LogWriterI18n)

Example 8 with LogWriterI18n

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

the class TransactionImpl method setRollbackOnly.

/**
   * Sets the status of the Global Transaction associated with this transaction to be RollBack only
   * 
   * @see javax.transaction.Transaction#setRollbackOnly()
   */
public void setRollbackOnly() throws IllegalStateException, SystemException {
    gtx = tm.getGlobalTransaction();
    if (gtx == null) {
        String exception = LocalizedStrings.TransactionImpl_TRANSACTIONIMPL_SETROLLBACKONLY_NO_GLOBAL_TRANSACTION_EXISTS.toLocalizedString();
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (writer.fineEnabled())
            writer.fine(exception);
        throw new SystemException(exception);
    }
    gtx.setRollbackOnly();
}
Also used : LogWriterI18n(org.apache.geode.i18n.LogWriterI18n)

Example 9 with LogWriterI18n

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

the class TransactionManagerImpl method begin.

/**
   * Create a new transaction and associate it with the current thread if none exists with the
   * current thread else throw an exception since nested transactions are not supported
   * 
   * Create a global transaction and associate the transaction created with the global transaction
   * 
   * @throws NotSupportedException - Thrown if the thread is already associated with a transaction
   *         and the Transaction Manager implementation does not support nested transactions.
   * @throws SystemException - Thrown if the transaction manager encounters an unexpected error
   *         condition.
   * 
   * @see javax.transaction.TransactionManager#begin()
   */
public void begin() throws NotSupportedException, SystemException {
    if (!isActive) {
        throw new SystemException(LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGER_INVALID.toLocalizedString());
    }
    LogWriterI18n log = TransactionUtils.getLogWriterI18n();
    if (log.fineEnabled()) {
        log.fine("TransactionManager.begin() invoked");
    }
    Thread thread = Thread.currentThread();
    if (transactionMap.get(thread) != null) {
        String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_BEGIN_NESTED_TRANSACTION_IS_NOT_SUPPORTED.toLocalizedString();
        if (VERBOSE)
            log.fine(exception);
        throw new NotSupportedException(exception);
    }
    try {
        TransactionImpl transaction = new TransactionImpl();
        transactionMap.put(thread, transaction);
        GlobalTransaction globalTransaction = new GlobalTransaction();
        globalTransactionMap.put(transaction, globalTransaction);
        globalTransaction.addTransaction(transaction);
        globalTransaction.setStatus(Status.STATUS_ACTIVE);
    } catch (Exception e) {
        String exception = LocalizedStrings.TransactionManagerImpl_BEGIN__SYSTEMEXCEPTION_DUE_TO_0.toLocalizedString(new Object[] { e });
        if (log.severeEnabled())
            log.severe(LocalizedStrings.TransactionManagerImpl_BEGIN__SYSTEMEXCEPTION_DUE_TO_0, new Object[] { e });
        throw new SystemException(exception);
    }
}
Also used : SystemException(javax.transaction.SystemException) LogWriterI18n(org.apache.geode.i18n.LogWriterI18n) NotSupportedException(javax.transaction.NotSupportedException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) InvalidTransactionException(javax.transaction.InvalidTransactionException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) CancelException(org.apache.geode.CancelException)

Example 10 with LogWriterI18n

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

the class TransactionManagerImpl method resume.

/**
   * @see javax.transaction.TransactionManager#resume(javax.transaction.Transaction)
   */
public void resume(Transaction txn) throws InvalidTransactionException, IllegalStateException, SystemException {
    if (!isActive) {
        throw new SystemException(LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGER_INVALID.toLocalizedString());
    }
    if (txn == null) {
        String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_RESUME_CANNOT_RESUME_A_NULL_TRANSACTION.toLocalizedString();
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (VERBOSE)
            writer.fine(exception);
        throw new InvalidTransactionException(exception);
    }
    GlobalTransaction gtx = getGlobalTransaction(txn);
    if (gtx == null) {
        String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_RESUME_CANNOT_RESUME_A_NULL_TRANSACTION.toLocalizedString();
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (VERBOSE)
            writer.fine(exception);
        throw new InvalidTransactionException(exception);
    }
    gtx.resume();
    try {
        Thread thread = Thread.currentThread();
        transactionMap.put(thread, txn);
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (writer.infoEnabled())
            writer.info(LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPLRESUMETRANSACTION_RESUMED);
    } catch (Exception e) {
        String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_RESUME_ERROR_IN_LISTING_THREAD_TO_TRANSACTION_MAP_DUE_TO_0.toLocalizedString(e);
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (VERBOSE)
            writer.fine(exception);
        throw new SystemException(exception);
    }
}
Also used : SystemException(javax.transaction.SystemException) LogWriterI18n(org.apache.geode.i18n.LogWriterI18n) InvalidTransactionException(javax.transaction.InvalidTransactionException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) InvalidTransactionException(javax.transaction.InvalidTransactionException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) CancelException(org.apache.geode.CancelException)

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