https://issues.apache.org/activemq/browse/AMQ-2950 - prevent ConcurrentModificationException when removing the connection

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1037675 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bosanac Dejan 2010-11-22 11:34:39 +00:00
parent fe1bf48a1d
commit fd4e13634b
1 changed files with 20 additions and 13 deletions

View File

@ -16,11 +16,8 @@
*/
package org.apache.activemq.broker;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import javax.jms.JMSException;
@ -252,15 +249,25 @@ public class TransactionBroker extends BrokerFilter {
iter.remove();
}
synchronized (xaTransactions) {
// first find all txs that belongs to the connection
ArrayList<XATransaction> txs = new ArrayList<XATransaction>();
for (XATransaction tx : xaTransactions.values()) {
if (tx.getConnectionId().equals(info.getConnectionId()) && !tx.isPrepared()) {
txs.add(tx);
}
}
// then remove them
// two steps needed to avoid ConcurrentModificationException, from removeTransaction()
for (XATransaction tx : txs) {
try {
tx.rollback();
} catch (Exception e) {
LOG.warn("ERROR Rolling back disconnected client's xa transactions: ", e);
}
}
for (XATransaction tx : xaTransactions.values()) {
try {
if (tx.getConnectionId().equals(info.getConnectionId()) && !tx.isPrepared()) {
tx.rollback();
}
} catch (Exception e) {
LOG.warn("ERROR Rolling back disconnected client's xa transactions: ", e);
}
}
next.removeConnection(context, info, error);
}