if (trace)
{
log.trace("Inserted " + rows + " rows");
}
Message m = ref.getMessage();
synchronized (m)
{
if (!m.isPersisted())
{
if (psInsertMessage == null)
{
psInsertMessage = conn
.prepareStatement(getSQLStatement("INSERT_MESSAGE"));
}
// First time so add message
// And in case of clustered queues/topics, the message
// could possibly be already persisted on the different
// node
// so we persist also using the Conditional Update
if (trace)
{
log
.trace("Message does not already exist so inserting it");
}
storeMessage(m, psInsertMessage, true);
rows = psInsertMessage.executeUpdate();
if (trace)
{
log.trace("Inserted " + rows + " rows");
}
m.setPersisted(true);
messagesStored.add(m);
//We only cache the first id in a tx - this is enough to determine if the tx succeeded
if (first && m instanceof JBossMessage)
{
cacheID(conn, ((JBossMessage)m).getJMSMessageID());
first = false;
}
}
}
}
// Now the removes
for (Iterator i = refsToRemove.iterator(); i.hasNext();)
{
ChannelRefPair pair = (ChannelRefPair) i.next();
if (psDeleteReference == null)
{
psDeleteReference = conn
.prepareStatement(getSQLStatement("DELETE_MESSAGE_REF"));
}
removeReference(pair.channelID, pair.ref, psDeleteReference);
int rows = psDeleteReference.executeUpdate();
if (trace)
{
log.trace("Deleted " + rows + " references");
}
}
return null;
}
catch (SQLException e)
{
if (transactionDone)
{
String sqlState = e.getSQLState();
//23000(XA) or 23xxx (SQL:2003)
if (sqlState.startsWith("23"))
{
//this is a constraint violation. i.e. the record identified by the same key already exists
//This is fine, log a warning and go ahead.
log.warn("We encountered a after-commit problem however messages have already been inserted.");
return null;
}
}
throw e;
}
finally
{
closeStatement(psReference);
closeStatement(psDeleteReference);
closeStatement(psInsertMessage);
}
}
public void rollback()
{
if(messagesStored != null)
{
for (Iterator i = messagesStored.iterator(); i.hasNext();)
{
Message msg = (Message) i.next();
msg.setPersisted(false);
}
}
}
}