psReference.close();
psReference = null;
}
Message m = ref.getMessage();
if (!batch)
{
psInsertMessage = conn.prepareStatement(getSQLStatement("INSERT_MESSAGE"));
psIncMessage = conn.prepareStatement(getSQLStatement("INC_CHANNEL_COUNT"));
}
boolean added;
if (!m.isPersisted())
{
// First time so add message
storeMessage(m, psInsertMessage);
added = true;
m.setPersisted(true);
}
else
{
// Update message channel count
incrementChannelCount(m, psIncMessage);
added = false;
}
if (batch)
{
if (added)
{
psInsertMessage.addBatch();
if (trace) { log.trace("Message does not already exist so inserting it"); }
messageInsertsInBatch = true;
}
else
{
if (trace) { log.trace("Message already exists so updating count"); }
psIncMessage.addBatch();
messageUpdatesInBatch = true;
}
}
else
{
if (added)
{
if (trace) { log.trace("Message does not already exist so inserting it"); }
int rows = updateWithRetry(psInsertMessage);
if (trace) { log.trace("Inserted " + rows + " rows"); }
}
else
{
if (trace) { log.trace("Message already exists so updating count"); }
int rows = updateWithRetry(psIncMessage);
if (trace) { log.trace("Updated " + rows + " rows"); }
}
psInsertMessage.close();
psInsertMessage = null;
psIncMessage.close();
psIncMessage = null;
}
}
if (batch)
{
// Process the add batch
int[] rowsReference = updateWithRetryBatch(psReference);
if (trace) { logBatchUpdate(getSQLStatement("INSERT_MESSAGE_REF"), rowsReference, "inserted"); }
if (messageInsertsInBatch)
{
int[] rowsMessage = updateWithRetryBatch(psInsertMessage);
if (trace) { logBatchUpdate(getSQLStatement("INSERT_MESSAGE"), rowsMessage, "inserted"); }
}
if (messageUpdatesInBatch)
{
int[] rowsMessage = updateWithRetryBatch(psIncMessage);
if (trace) { logBatchUpdate(getSQLStatement("INC_CHANNEL_COUNT"), rowsMessage, "updated"); }
}
psReference.close();
psReference = null;
psInsertMessage.close();
psInsertMessage = null;
psIncMessage.close();
psIncMessage = null;
}
// Now the removes
psReference = null;
psDeleteMessage = null;
batch = usingBatchUpdates && refsToRemove.size() > 0;
if (batch)
{
psReference = conn.prepareStatement(getSQLStatement("DELETE_MESSAGE_REF"));
psDeleteMessage = conn.prepareStatement(getSQLStatement("DELETE_MESSAGE"));
psDecMessage = conn.prepareStatement(getSQLStatement("DEC_CHANNEL_COUNT"));
}
for(Iterator i = refsToRemove.iterator(); i.hasNext(); )
{
ChannelRefPair pair = (ChannelRefPair)i.next();
if (!batch)
{
psReference = conn.prepareStatement(getSQLStatement("DELETE_MESSAGE_REF"));
}
removeReference(pair.channelID, pair.ref, psReference);
if (batch)
{
psReference.addBatch();
}
else
{
int rows = updateWithRetry(psReference);
if (trace) { log.trace("Deleted " + rows + " rows"); }
psReference.close();
psReference = null;
}
if (!batch)
{
psDeleteMessage = conn.prepareStatement(getSQLStatement("DELETE_MESSAGE"));
psDecMessage = conn.prepareStatement(getSQLStatement("DEC_CHANNEL_COUNT"));
}
Message m = pair.ref.getMessage();
// Update the channel count
decrementChannelCount(m, psDecMessage);