PageRenderTime 37ms CodeModel.GetById 27ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/remote/protocol/versionone/XidTransactionCommitTask.java

#
Java | 148 lines | 89 code | 21 blank | 38 comment | 11 complexity | b07db748068efc1f3e5aa8197e12c944 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1/*
  2 * JBoss, Home of Professional Open Source.
  3 * Copyright 2011, Red Hat, Inc., and individual contributors
  4 * as indicated by the @author tags. See the copyright.txt file in the
  5 * distribution for a full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.jboss.as.ejb3.remote.protocol.versionone;
 24
 25import javax.transaction.HeuristicCommitException;
 26import javax.transaction.HeuristicMixedException;
 27import javax.transaction.HeuristicRollbackException;
 28import javax.transaction.RollbackException;
 29import javax.transaction.SystemException;
 30import javax.transaction.Transaction;
 31import javax.transaction.xa.XAException;
 32import javax.transaction.xa.Xid;
 33
 34import com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction;
 35import com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinationManager;
 36import org.jboss.as.ejb3.EjbLogger;
 37import org.jboss.as.ejb3.remote.EJBRemoteTransactionsRepository;
 38import org.jboss.ejb.client.XidTransactionID;
 39import org.jboss.logging.Logger;
 40import org.jboss.marshalling.MarshallerFactory;
 41import org.jboss.remoting3.Channel;
 42
 43/**
 44 * @author Jaikiran Pai
 45 */
 46class XidTransactionCommitTask extends XidTransactionManagementTask {
 47
 48    private static final Logger logger = Logger.getLogger(XidTransactionCommitTask.class);
 49
 50    private final boolean onePhaseCommit;
 51
 52    XidTransactionCommitTask(final TransactionRequestHandler txRequestHandler, final EJBRemoteTransactionsRepository transactionsRepository,
 53                             final MarshallerFactory marshallerFactory, final XidTransactionID xidTransactionID, final Channel channel,
 54                             final short invocationId, final boolean onePhaseCommit) {
 55        super(txRequestHandler, transactionsRepository, marshallerFactory, xidTransactionID, channel, invocationId);
 56        this.onePhaseCommit = onePhaseCommit;
 57    }
 58
 59    @Override
 60    protected void manageTransaction() throws Throwable {
 61        // first associate the tx on this thread, by resuming the tx
 62        final Transaction transaction = this.transactionsRepository.removeTransaction(this.xidTransactionID);
 63        if(transaction == null) {
 64            if(EjbLogger.EJB3_INVOCATION_LOGGER.isDebugEnabled()) {
 65                //this happens if no ejb invocations where made within the TX
 66                EjbLogger.EJB3_INVOCATION_LOGGER.debug("Not committing transaction " + this.xidTransactionID + " as is was not found on the server");
 67            }
 68            return;
 69        }
 70        this.resumeTransaction(transaction);
 71        // now commit
 72        final Xid xid = this.xidTransactionID.getXid();
 73        // Courtesy: com.arjuna.ats.internal.jta.transaction.arjunacore.jca.XATerminatorImple
 74        try {
 75            // get the subordinate tx
 76            final SubordinateTransaction subordinateTransaction = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
 77
 78            if (subordinateTransaction == null) {
 79                throw new XAException(XAException.XAER_INVAL);
 80            }
 81
 82            if (subordinateTransaction.activated()) {
 83                if (this.onePhaseCommit) {
 84                    subordinateTransaction.doOnePhaseCommit();
 85                } else {
 86                    subordinateTransaction.doCommit();
 87                }
 88                // remove the tx
 89                SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
 90            } else {
 91                throw new XAException(XAException.XA_RETRY);
 92            }
 93
 94        } catch (RollbackException e) {
 95            // remove the tx
 96            SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
 97            final XAException xaException = new XAException(XAException.XA_RBROLLBACK);
 98            xaException.initCause(e);
 99            throw xaException;
100
101        } catch (XAException ex) {
102            // resource hasn't had a chance to recover yet
103            if (ex.errorCode != XAException.XA_RETRY) {
104                // remove tx
105                SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
106            }
107            throw ex;
108
109        } catch (HeuristicRollbackException ex) {
110            final XAException xaException = new XAException(XAException.XA_HEURRB);
111            xaException.initCause(ex);
112            throw xaException;
113
114        } catch (HeuristicMixedException ex) {
115            final XAException xaException = new XAException(XAException.XA_HEURMIX);
116            xaException.initCause(ex);
117            throw xaException;
118
119        } catch (final HeuristicCommitException ex) {
120            final XAException xaException = new XAException(XAException.XA_HEURCOM);
121            xaException.initCause(ex);
122            throw xaException;
123
124        } catch (final IllegalStateException ex) {
125            // remove tx
126            SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
127
128            final XAException xaException = new XAException(XAException.XAER_NOTA);
129            xaException.initCause(ex);
130            throw xaException;
131
132        } catch (SystemException ex) {
133            // remove tx
134            SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
135
136            final XAException xaException = new XAException(XAException.XAER_RMERR);
137            xaException.initCause(ex);
138            throw xaException;
139
140
141        } finally {
142            // disassociate the tx that was associated (resumed) on this thread.
143            // This needs to be done explicitly because the SubOrdinationManager isn't responsible
144            // for clearing the tx context from the thread
145            this.transactionsRepository.getTransactionManager().suspend();
146        }
147    }
148}