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.ra; 018 019import javax.jms.JMSException; 020import javax.transaction.xa.XAException; 021import javax.transaction.xa.XAResource; 022import javax.transaction.xa.Xid; 023 024import org.apache.activemq.TransactionContext; 025import org.apache.activemq.command.TransactionId; 026import org.apache.activemq.transaction.Synchronization; 027 028/** 029 * Allows us to switch between using a shared transaction context, or using a 030 * local transaction context. 031 * 032 * 033 */ 034public class ManagedTransactionContext extends TransactionContext { 035 036 private final TransactionContext sharedContext; 037 private boolean useSharedTxContext; 038 039 public ManagedTransactionContext(TransactionContext sharedContext) { 040 super(sharedContext.getConnection()); 041 this.sharedContext = sharedContext; 042 setLocalTransactionEventListener(sharedContext.getLocalTransactionEventListener()); 043 } 044 045 public void setUseSharedTxContext(boolean enable) throws JMSException { 046 if (isInLocalTransaction() || isInXATransaction()) { 047 throw new JMSException("The resource is already being used in transaction context."); 048 } 049 useSharedTxContext = enable; 050 } 051 052 public void begin() throws JMSException { 053 if (useSharedTxContext) { 054 sharedContext.begin(); 055 } else { 056 super.begin(); 057 } 058 } 059 060 public void commit() throws JMSException { 061 if (useSharedTxContext) { 062 sharedContext.commit(); 063 } else { 064 super.commit(); 065 } 066 } 067 068 public void commit(Xid xid, boolean onePhase) throws XAException { 069 if (useSharedTxContext) { 070 sharedContext.commit(xid, onePhase); 071 } else { 072 super.commit(xid, onePhase); 073 } 074 } 075 076 public void end(Xid xid, int flags) throws XAException { 077 if (useSharedTxContext) { 078 sharedContext.end(xid, flags); 079 } else { 080 super.end(xid, flags); 081 } 082 } 083 084 public void forget(Xid xid) throws XAException { 085 if (useSharedTxContext) { 086 sharedContext.forget(xid); 087 } else { 088 super.forget(xid); 089 } 090 } 091 092 public TransactionId getTransactionId() { 093 if (useSharedTxContext) { 094 return sharedContext.getTransactionId(); 095 } else { 096 return super.getTransactionId(); 097 } 098 } 099 100 public int getTransactionTimeout() throws XAException { 101 if (useSharedTxContext) { 102 return sharedContext.getTransactionTimeout(); 103 } else { 104 return super.getTransactionTimeout(); 105 } 106 } 107 108 public boolean isInLocalTransaction() { 109 if (useSharedTxContext) { 110 return sharedContext.isInLocalTransaction(); 111 } else { 112 return super.isInLocalTransaction(); 113 } 114 } 115 116 public boolean isRollbackOnly() { 117 return sharedContext.isRollbackOnly() || super.isRollbackOnly(); 118 } 119 120 public boolean isInXATransaction() { 121 if (useSharedTxContext) { 122 // context considers ended XA transactions as active, so just check for presence 123 // of tx when it is shared 124 return sharedContext.isInTransaction(); 125 } else { 126 return super.isInXATransaction(); 127 } 128 } 129 130 @Override 131 public boolean isInTransaction() { 132 return isInXATransaction() || isInLocalTransaction(); 133 } 134 135 public boolean isSameRM(XAResource xaResource) throws XAException { 136 if (useSharedTxContext) { 137 return sharedContext.isSameRM(xaResource); 138 } else { 139 return super.isSameRM(xaResource); 140 } 141 } 142 143 public int prepare(Xid xid) throws XAException { 144 if (useSharedTxContext) { 145 return sharedContext.prepare(xid); 146 } else { 147 return super.prepare(xid); 148 } 149 } 150 151 public Xid[] recover(int flag) throws XAException { 152 if (useSharedTxContext) { 153 return sharedContext.recover(flag); 154 } else { 155 return super.recover(flag); 156 } 157 } 158 159 public void rollback() throws JMSException { 160 if (useSharedTxContext) { 161 sharedContext.rollback(); 162 } else { 163 super.rollback(); 164 } 165 } 166 167 public void rollback(Xid xid) throws XAException { 168 if (useSharedTxContext) { 169 sharedContext.rollback(xid); 170 } else { 171 super.rollback(xid); 172 } 173 } 174 175 public boolean setTransactionTimeout(int seconds) throws XAException { 176 if (useSharedTxContext) { 177 return sharedContext.setTransactionTimeout(seconds); 178 } else { 179 return super.setTransactionTimeout(seconds); 180 } 181 } 182 183 public void start(Xid xid, int flags) throws XAException { 184 if (useSharedTxContext) { 185 sharedContext.start(xid, flags); 186 } else { 187 super.start(xid, flags); 188 } 189 } 190 191 public void addSynchronization(Synchronization s) { 192 if (useSharedTxContext) { 193 sharedContext.addSynchronization(s); 194 } else { 195 super.addSynchronization(s); 196 } 197 } 198}