001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.transaction; 018 019import java.io.IOException; 020import javax.transaction.xa.XAException; 021import org.apache.activemq.broker.ConnectionContext; 022import org.apache.activemq.command.LocalTransactionId; 023import org.apache.activemq.command.TransactionId; 024import org.apache.activemq.store.TransactionStore; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028/** 029 * 030 */ 031public class LocalTransaction extends Transaction { 032 033 private static final Logger LOG = LoggerFactory.getLogger(LocalTransaction.class); 034 035 private final TransactionStore transactionStore; 036 private final LocalTransactionId xid; 037 private final ConnectionContext context; 038 039 public LocalTransaction(TransactionStore transactionStore, LocalTransactionId xid, ConnectionContext context) { 040 this.transactionStore = transactionStore; 041 this.xid = xid; 042 this.context = context; 043 } 044 045 @Override 046 public void commit(boolean onePhase) throws XAException, IOException { 047 if (LOG.isDebugEnabled()) { 048 LOG.debug("commit: " + xid 049 + " syncCount: " + size()); 050 } 051 052 // Get ready for commit. 053 try { 054 prePrepare(); 055 } catch (XAException e) { 056 throw e; 057 } catch (Throwable e) { 058 LOG.warn("COMMIT FAILED: ", e); 059 rollback(); 060 // Let them know we rolled back. 061 XAException xae = new XAException("COMMIT FAILED: Transaction rolled back"); 062 xae.errorCode = XAException.XA_RBOTHER; 063 xae.initCause(e); 064 throw xae; 065 } 066 067 setState(Transaction.FINISHED_STATE); 068 context.getTransactions().remove(xid); 069 try { 070 transactionStore.commit(getTransactionId(), false, preCommitTask, postCommitTask); 071 this.waitPostCommitDone(postCommitTask); 072 } catch (Throwable t) { 073 LOG.warn("Store COMMIT FAILED: ", t); 074 rollback(); 075 XAException xae = new XAException("STORE COMMIT FAILED: Transaction rolled back"); 076 xae.errorCode = XAException.XA_RBOTHER; 077 xae.initCause(t); 078 throw xae; 079 } 080 } 081 082 @Override 083 public void rollback() throws XAException, IOException { 084 085 if (LOG.isDebugEnabled()) { 086 LOG.debug("rollback: " + xid 087 + " syncCount: " + size()); 088 } 089 setState(Transaction.FINISHED_STATE); 090 context.getTransactions().remove(xid); 091 transactionStore.rollback(getTransactionId()); 092 try { 093 fireAfterRollback(); 094 } catch (Throwable e) { 095 LOG.warn("POST ROLLBACK FAILED: ", e); 096 XAException xae = new XAException("POST ROLLBACK FAILED"); 097 xae.errorCode = XAException.XAER_RMERR; 098 xae.initCause(e); 099 throw xae; 100 } 101 } 102 103 @Override 104 public int prepare() throws XAException { 105 XAException xae = new XAException("Prepare not implemented on Local Transactions"); 106 xae.errorCode = XAException.XAER_RMERR; 107 throw xae; 108 } 109 110 @Override 111 public TransactionId getTransactionId() { 112 return xid; 113 } 114 115 @Override 116 public Logger getLog() { 117 return LOG; 118 } 119}