LocalTransactionCommand xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.service.impl;
    19
    20  import org.apache.commons.logging.Log;
    21  import org.apache.commons.logging.LogFactory;
    22  import org.codehaus.activemq.broker.Broker;
    23
    24  import javax.transaction.xa.XAException;
    25  import java.util.Map;
    26
    27  /***
    28   * @version $Revision: 1.1 $
    29   */
    30  public class LocalTransactionCommand extends AbstractTransaction {
    31      private static final long serialVersionUID = -5754338187296859149L;
    32      private static final Log log = LogFactory.getLog(LocalTransactionCommand.class);
    33
    34      private Map localTxs;
    35      private Object txid;
    36
    37
    38      public LocalTransactionCommand(Broker broker, Map localTxs, Object txid) {
    39          super(broker);
    40          this.localTxs = localTxs;
    41          this.txid = txid;
    42      }
    43
    44      public void commit(boolean onePhase) throws XAException {
    45          // Get ready for commit.
    46          try {
    47              prePrepare();
    48          }
    49          catch (XAException e) {
    50              throw e;
    51          }
    52          catch (Throwable e) {
    53              log.warn("COMMIT FAILED: ", e);
    54              rollback();
    55              // Let them know we rolled back.
    56              XAException xae = new XAException("COMMIT FAILED: Transaction rolled back.");
    57              xae.errorCode = XAException.XA_RBOTHER;
    58              xae.initCause(e);
    59              throw xae;
    60          }
    61
    62          setState(AbstractTransaction.FINISHED_STATE);
    63          localTxs.remove(txid);
    64
    65          try {
    66              postCommit();
    67          }
    68          catch (Throwable e) {
    69              // I guess this could happen.  Post commit task failed
    70              // to execute properly.
    71              log.warn("POST COMMIT FAILED: ", e);
    72              XAException xae = new XAException("POST COMMIT FAILED");
    73              xae.errorCode = XAException.XAER_RMERR;
    74              xae.initCause(e);
    75              throw xae;
    76          }
    77      }
    78
    79      public void rollback() throws XAException {
    80
    81          setState(AbstractTransaction.FINISHED_STATE);
    82          localTxs.remove(txid);
    83
    84          try {
    85              postRollback();
    86          }
    87          catch (Throwable e) {
    88              log.warn("POST ROLLBACK FAILED: ", e);
    89              XAException xae = new XAException("POST ROLLBACK FAILED");
    90              xae.errorCode = XAException.XAER_RMERR;
    91              xae.initCause(e);
    92              throw xae;
    93          }
    94      }
    95
    96      public int prepare() throws XAException {
    97          XAException xae = new XAException("Prepare not implemented on Local Transactions.");
    98          xae.errorCode = XAException.XAER_RMERR;
    99          throw xae;
    100     }
    101
    102 }