boolean messagesInBatch = false;
boolean messageInsertsInBatch = false; boolean messageUpdatesInBatch = false;
   public void addReferences(long channelID, List references) throws Exception   {      Connection conn = null;      PreparedStatement psReference = null;      PreparedStatement psMessage = null;      TransactionWrapper wrap = new TransactionWrapper();      try      {         conn = ds.getConnection();         Iterator iter = references.iterator();         boolean messagesInBatch = false;         if (usingBatchUpdates)         {            psReference = conn.prepareStatement(insertMessageRef);            psMessage = conn.prepareStatement(insertMessage);         }         while (iter.hasNext())         {            //We may need to persist the message itself             MessageReference ref = (MessageReference) iter.next();            if (!usingBatchUpdates)            {               psReference = conn.prepareStatement(insertMessageRef);            }            //Now store the reference            addReference(channelID, ref, psReference);            if (usingBatchUpdates)            {               psReference.addBatch();            }            else            {               int rows = psReference.executeUpdate();               if (trace)               {                  log.trace("Inserted " + rows + " rows");               }               psReference.close();               psReference = null;            }            if (!usingBatchUpdates)            {               psMessage = conn.prepareStatement(insertMessage);            }            //Maybe we need to persist the message itself            boolean added = checkStoreMessage(ref, psMessage);            if (usingBatchUpdates)            {               if (added)               {                  psMessage.addBatch();                  messagesInBatch = true;               }            }            else            {               if (added)               {                  int rows = psMessage.executeUpdate();                  if (trace)                  {                     log.trace("Inserted " + rows + " rows");                  }               }               psMessage.close();               psMessage = null;            }         }         if (usingBatchUpdates)         {            int[] rowsReference = psReference.executeBatch();            if (trace)            {               logBatchUpdate(insertMessageRef, rowsReference, "inserted");            }            if (messagesInBatch)            {               int[] rowsMessage = psMessage.executeBatch();               if (trace)               {                  logBatchUpdate(insertMessage, rowsMessage, "inserted");               }            }            psReference.close();            psReference = null;            psMessage.close();            psMessage = null;         }      }      catch (Exception e)      {         wrap.exceptionOccurred();         throw e;      }      finally      {         if (psReference != null)         {            try            {               psReference.close();            }            catch (Throwable t)            {            }         }         if (psMessage != null)         {            try            {               psMessage.close();            }            catch (Throwable t)            {            }         }         if (conn != null)         {            try            {               conn.close();            }            catch (Throwable t)            {            }         }         wrap.end();      }   }