{ Message m = ref.getMessage();
{  LockMap.instance.obtainLock(m);
   public void addReference(long channelID, MessageReference ref, Transaction tx) throws Exception   {            if (tx != null)      {         //In a tx so we just add the ref in the tx in memory for now         TransactionCallback callback = getCallback(tx);         callback.addReferenceToAdd(channelID, ref);      }      else      {                  //No tx so add the ref directly in the db                  TransactionWrapper wrap = new TransactionWrapper();                  PreparedStatement psReference = null;         PreparedStatement psMessage = null;                  Connection conn = ds.getConnection();         try         {            Message m = ref.getMessage();                                                psReference = conn.prepareStatement(insertMessageRef);                        //Add the reference            addReference(channelID, ref, psReference, true);                        int rows = psReference.executeUpdate();                        if (trace)            {               log.trace("Inserted " + rows + " rows");            }                           synchronized (m)            {                                m.incPersistentChannelCount();                              if (m.getPersistentChannelCount() == 1)               {                  //First time so persist the message                  psMessage = conn.prepareStatement(insertMessage);                                    storeMessage(m, psMessage);                       }               else               {                  //Update the message's channel count                  psMessage = conn.prepareStatement(updateMessageChannelCount);                                    updateMessageChannelCount(m, psMessage);               }                                             rows = psMessage.executeUpdate();               if (trace)               {                  log.trace("Inserted/updated " + rows + " rows");               }                           }         }         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();         }            }   }