Index: src/edu/ucsb/nceas/metacat/MetacatReplication.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetacatReplication.java	(revision 682)
+++ src/edu/ucsb/nceas/metacat/MetacatReplication.java	(revision 683)
@@ -190,9 +190,11 @@
                                           HttpServletResponse response)
   {
     String subaction = ((String[])params.get("subaction"))[0];
+    Connection conn = null;
     try
     {
-      Connection conn = util.openDBConnection();
+      conn = MetacatReplication.getDBConnection("MetacatReplication." + 
+                                                "handleServerControlRequest");
       PreparedStatement pstmt = null;
       if(subaction.equals("add"))
       {
@@ -331,7 +333,11 @@
       String user = (String)docinfoHash.get("user_owner");
       String group = new String(user);
       //right now the user and group are the same.
-      Connection conn = util.openDBConnection();
+      Connection conn = null;
+      
+      conn = MetacatReplication.getDBConnection("MetacatReplication." +
+                                                "handleForceReplicateRequest");
+    
       DocumentImpl.write(conn, new StringReader(xmldoc), null, dbaction, docid, 
                          user, group, serverCode, override);
       MetacatReplication.replLog("document " + docid + " added to DB with " +
@@ -355,9 +361,11 @@
   private void handleGetLockRequest(PrintWriter out, Hashtable params,
                                     HttpServletResponse response)
   {
+    Connection conn = null;
     try
     {
-      Connection conn = util.openDBConnection();
+      conn = MetacatReplication.getDBConnection("MetacatReplication." +
+                                                "handleGetLockRequest");
       String docid = ((String[])params.get("docid"))[0];
       String remoteRev = ((String[])params.get("updaterev"))[0];
       DocumentImpl requestDoc = new DocumentImpl(conn, docid);
@@ -413,9 +421,11 @@
   {
     String docid = ((String[])(params.get("docid")))[0];
     StringBuffer sb = new StringBuffer();
+    Connection conn = null;
     try
     {
-      Connection conn = util.openDBConnection();
+      conn = MetacatReplication.getDBConnection("MetacatReplication." +
+                                                "handleGetDocumentInfoRequest");
       DocumentImpl doc = new DocumentImpl(conn, docid);
       sb.append("<documentinfo><docid>").append(docid);
       sb.append("</docid><docname>").append(doc.getDocname());
@@ -445,11 +455,13 @@
   private void handleGetDocumentRequest(PrintWriter out, Hashtable params, 
                                         HttpServletResponse response)
   {
+    Connection conn = null;
     try
     {
       String docid = ((String[])(params.get("docid")))[0];
-      MetaCatUtil.debugMessage("incoming get request for document: " + docid);
-      Connection conn = util.openDBConnection();
+      System.out.println("incoming get request for document: " + docid);
+      conn = MetacatReplication.getDBConnection("MetacatReplication." +
+                                                "handleGetDocumentRequest");
       DocumentImpl di = new DocumentImpl(conn, docid);
       response.setContentType("text/xml");
       out.print(di.toString());
@@ -461,6 +473,7 @@
     {
       System.out.println("error getting document from MetacatReplication." +
                          "handlGetDocumentRequest " + e.getMessage());
+      e.printStackTrace(System.out);
     }
     
   }
@@ -482,6 +495,7 @@
   private void handleUpdateRequest(PrintWriter out, Hashtable params, 
                                     HttpServletResponse response)
   {
+    Connection conn = null;
     try
     {
       System.out.println("received update request");
@@ -504,7 +518,8 @@
       delsql.append("xml_revisions where docid not in (select docid from ");
       delsql.append("xml_documents) and server_location = 1");
       
-      Connection conn = util.openDBConnection();
+      conn = MetacatReplication.getDBConnection("MetacatReplication." +
+                                                "handleUpdateRequest");
       PreparedStatement pstmt = conn.prepareStatement(docsql.toString());
       pstmt.execute();
       ResultSet rs = pstmt.getResultSet();
@@ -511,23 +526,27 @@
       boolean tablehasrows = rs.next();
       while(tablehasrows)
       {
-        if(!rs.getString(3).equals(util.getOption("packagedoctype")))
-        { //if this is a package file, put it at the end
-          //because if a package file is read before all of the files it
-          //refers to are loaded then there is an error
-          doclist.append("<updatedDocument>");
-          doclist.append("<docid>").append(rs.getString(1));
-          doclist.append("</docid><rev>").append(rs.getInt(2));
-          doclist.append("</rev>");
-          doclist.append("</updatedDocument>");
+        String recordDoctype = rs.getString(3);
+        if(!recordDoctype.equals("BIN"))
+        { //don't replicate data files
+          if(!recordDoctype.equals(util.getOption("packagedoctype")))
+          { //if this is a package file, put it at the end
+            //because if a package file is read before all of the files it
+            //refers to are loaded then there is an error
+            doclist.append("<updatedDocument>");
+            doclist.append("<docid>").append(rs.getString(1));
+            doclist.append("</docid><rev>").append(rs.getInt(2));
+            doclist.append("</rev>");
+            doclist.append("</updatedDocument>");
+          }
+          else
+          { //the package files are saved to be put into the xml later.
+            Vector v = new Vector();
+            v.add(new String(rs.getString(1)));
+            v.add(new Integer(rs.getInt(2)));
+            packageFiles.add(new Vector(v));
+          }
         }
-        else
-        { //the package files are saved to be put into the xml later.
-          Vector v = new Vector();
-          v.add(new String(rs.getString(1)));
-          v.add(new Integer(rs.getInt(2)));
-          packageFiles.add(new Vector(v));
-        }
         tablehasrows = rs.next();
       }
       
@@ -599,8 +618,9 @@
     Connection conn = null;
     PreparedStatement pstmt = null;
     try
-    {
-      conn = util.openDBConnection();
+    { 
+      conn = MetacatReplication.getDBConnection("MetacatReplication." +
+                                                "handleGetCatalogRequest");
       pstmt = conn.prepareStatement("select entry_type, " +
                               "source_doctype, target_doctype, public_id, " +
                               "system_id from xml_catalog");
@@ -702,9 +722,11 @@
   public static String getServer(int serverCode)
   {
     //System.out.println("serverid: " + serverCode);
+    Connection conn = null;
     try
     {
-      Connection conn = util.openDBConnection();
+      conn = MetacatReplication.getDBConnection("MetacatReplication." +
+                                                "getServer");
       String sql = new String("select server from " +
                               "xml_replication where serverid = " + 
                               serverCode);
@@ -742,7 +764,7 @@
     PreparedStatement pstmt = null;
     try
     {
-      conn = util.openDBConnection();
+      conn = MetacatReplication.getDBConnection("MetacatReplication.getServerCode");
       pstmt = conn.prepareStatement("select serverid from " +
                                          "xml_replication where server " +
                                          "like '" + server + "'");
@@ -838,7 +860,7 @@
     PreparedStatement pstmt = null;
     try
     {
-      conn = util.openDBConnection();
+      conn = MetacatReplication.getDBConnection("MetacatReplication.replToServer");
       pstmt = conn.prepareStatement("select replicate from " + 
                                     "xml_replication where server like '" +
                                      server + "'");
@@ -880,4 +902,51 @@
     return false;
     //the default if this server does not exist is to not replicate to it.
   }
+  
+  /**
+   * A method for redundantly trying to connect.  this method will attempt to 
+   * connect to the DB 3 times before failing.
+   * @param methodname the methodname from which this method is called.
+   */
+  public static Connection getDBConnection(String methodname) throws Exception
+  {
+    Connection conn = null;
+    try
+    {
+      try
+      { //this connection is prone to error for some reason so we 
+        //try to connect it three times before quiting.
+        conn = util.openDBConnection();
+        return conn;
+      }
+      catch(SQLException sqle)
+      {
+        try
+        {
+          conn = util.openDBConnection();
+          return conn;
+        }
+        catch(SQLException sqlee)
+        {
+          try
+          {
+            conn = util.openDBConnection();
+            return conn;
+          }
+          catch(SQLException sqleee)
+          {
+            System.out.println("error getting db connection in " + 
+                               methodname +  ": " +
+                               sqleee.getMessage());
+            return conn;
+          }
+        }
+      }
+    }
+    catch(Exception e)
+    {
+      throw new java.lang.Exception("error in " + methodname + ": " + 
+                          e.getMessage());
+    }
+  }
 }
Index: src/edu/ucsb/nceas/metacat/ReplicationHandler.java
===================================================================
--- src/edu/ucsb/nceas/metacat/ReplicationHandler.java	(revision 682)
+++ src/edu/ucsb/nceas/metacat/ReplicationHandler.java	(revision 683)
@@ -82,10 +82,34 @@
     //xml_documents with an update_date > last_checked
     try
     {
-      conn = util.openDBConnection();
+      try
+      { //this connection is prone to error for some reason so we 
+        //try to connect it three times before quiting.
+        conn = util.openDBConnection();
+      }
+      catch(SQLException sqle)
+      {
+        try
+        {
+          conn = util.openDBConnection();
+        }
+        catch(SQLException sqlee)
+        {
+          try
+          {
+            conn = util.openDBConnection();
+          }
+          catch(SQLException sqleee)
+          {
+            System.out.println("error getting db connection in " + 
+                               "ReplicationHandler.run: " +
+                               sqleee.getMessage());
+          }
+        }
+      }
       serverList = buildServerList(conn);
-      update(serverList, conn);
-      updateCatalog(serverList, conn);
+      update(serverList);
+      updateCatalog(serverList);
       conn.close();
     }
     catch (Exception e)
@@ -97,7 +121,7 @@
   /**
    * Method that uses revision taging for replication instead of update_date.
    */
-  private void update(Hashtable serverList, Connection conn)
+  private void update(Hashtable serverList)
   {
     /*
      Pseudo-algorithm
@@ -112,6 +136,42 @@
        (this info is theoretically not needed using this system but probably 
        should be kept anyway)
     */
+    
+    Connection conn = null;
+    try
+    {
+      try
+      { //this connection is prone to error for some reason so we 
+        //try to connect it three times before quiting.
+        conn = util.openDBConnection();
+      }
+      catch(SQLException sqle)
+      {
+        try
+        {
+          conn = util.openDBConnection();
+        }
+        catch(SQLException sqlee)
+        {
+          try
+          {
+            conn = util.openDBConnection();
+          }
+          catch(SQLException sqleee)
+          {
+            System.out.println("error getting db connection in " + 
+                               "ReplicationHandler.update: " +
+                               sqleee.getMessage());
+          }
+        }
+      }
+    }
+    catch(Exception e)
+    {
+      System.out.println("error in ReplicationHandler.update: " + 
+                          e.getMessage());
+    }
+    
     Enumeration keys;
     String server;
     String update;
@@ -285,10 +345,36 @@
   /**
    * updates xml_catalog with entries from other servers.
    */
-  private void updateCatalog(Hashtable serverList, Connection conn)
+  private void updateCatalog(Hashtable serverList)
   {
+    Connection conn = null;
     try
     {
+      try
+      { //this connection is prone to error for some reason so we 
+        //try to connect it three times before quiting.
+        conn = util.openDBConnection();
+      }
+      catch(SQLException sqle)
+      {
+        try
+        {
+          conn = util.openDBConnection();
+        }
+        catch(SQLException sqlee)
+        {
+          try
+          {
+            conn = util.openDBConnection();
+          }
+          catch(SQLException sqleee)
+          {
+            System.out.println("error getting db connection in " + 
+                               "ReplicationHandler.update: " +
+                               sqleee.getMessage());
+          }
+        }
+      }
       String server;
       Enumeration keys = serverList.keys();
       while(keys.hasMoreElements())
Index: src/edu/ucsb/nceas/metacat/RelationHandler.java
===================================================================
--- src/edu/ucsb/nceas/metacat/RelationHandler.java	(revision 682)
+++ src/edu/ucsb/nceas/metacat/RelationHandler.java	(revision 683)
@@ -43,14 +43,14 @@
     //this.conn = conn;
     try
     {
-      this.conn = util.openDBConnection();
+      conn = MetacatReplication.getDBConnection("RelationHandler." +
+                                                "relationHandler");
     }
     catch(Exception e)
     {
-      System.out.println("error opening connection in " + 
-                         "relationHandler.relationHandler: " + e.getMessage());
+      System.out.println("unable to get db connection in relationhandler." + 
+                         "relationhandler: " + e.getMessage());
     }
-    
     this.xmldoc = xmldoc;
     if(xmldoc.getDoctype().equals(util.getOption("packagedoctype")))
     { //make sure this doctype is a package document then run the thread
@@ -62,11 +62,11 @@
   
   /**
    * The thread handler
-   */
+   */ 
   public void run()
   {
+    Connection dbconn = null;
     String docid = xmldoc.getDocID();
-
     // deletes all of the relations with a docid of @docid.
     deleteRelations(docid);
     //pseudo-code algorithm
@@ -77,7 +77,9 @@
     //    add a new row to xml_relation that represents this new relation
     try
     {
-      PreparedStatement pstmt = conn.prepareStatement(
+      dbconn = MetacatReplication.getDBConnection("RelationHandler." +
+                                                  "run");
+      PreparedStatement pstmt = dbconn.prepareStatement(
                                 QuerySpecification.printPackageSQL(docid));
       pstmt.execute();
       
@@ -103,7 +105,7 @@
           paramDocid = subject;
         }
         
-        DocumentImpl subDoc = new DocumentImpl(conn, paramDocid);
+        DocumentImpl subDoc = new DocumentImpl(dbconn, paramDocid);
         subjectDoctype = subDoc.getDoctype();
         String relationship = rs.getString(2);
         String object = rs.getString(3);
@@ -110,7 +112,7 @@
       
         //compare r to each relation in xml_relation
         
-        pstmt = conn.prepareStatement("select subject, subdoctype, " +
+        pstmt = dbconn.prepareStatement("select subject, subdoctype, " +
                                       "relationship, object, objdoctype " +
                                       "from xml_relation");
         pstmt.execute();
@@ -139,7 +141,7 @@
             insertTransRelation.append(subject).append("', '");
             insertTransRelation.append(subjectDoctype).append("')");
             //System.out.println("sql1: " + insertTransRelation.toString());
-            pstmt = conn.prepareStatement(insertTransRelation.toString());
+            pstmt = dbconn.prepareStatement(insertTransRelation.toString());
             pstmt.execute(); 
             
             insertTransRelation = new StringBuffer();
@@ -155,7 +157,7 @@
             insertTransRelation.append(currentSub).append("', '");
             insertTransRelation.append(currentSubDoctype).append("')");
             //System.out.println("sql2: " + insertTransRelation.toString());
-            pstmt = conn.prepareStatement(insertTransRelation.toString());
+            pstmt = dbconn.prepareStatement(insertTransRelation.toString());
             pstmt.execute();
           }
           
@@ -177,7 +179,7 @@
             subDocid = (String)subMurlParams.get("docid");
             if(subMurl.getProtocol().equals("metacat"))
             {
-              subDoc = new DocumentImpl(conn, subDocid);
+              subDoc = new DocumentImpl(dbconn, subDocid);
               subDoctype = subDoc.getDoctype();
             }
           }
@@ -200,7 +202,7 @@
             objDocid = (String)objMurlParams.get("docid");
             if(objMurl.getProtocol().equals("metacat"))
             {
-              DocumentImpl objDoc = new DocumentImpl(conn, objDocid);
+              DocumentImpl objDoc = new DocumentImpl(dbconn, objDocid);
               objDoctype = objDoc.getDoctype();
             }
           }
@@ -228,29 +230,29 @@
         insertStmt.append(object).append("', '");
         insertStmt.append(objDoctype).append("')");
         
-        pstmt = conn.prepareStatement(insertStmt.toString());
+        pstmt = dbconn.prepareStatement(insertStmt.toString());
         pstmt.execute(); 
         
         hasmorerows = rs.next();
       }
       
-      conn.commit();
+      dbconn.commit();
       pstmt.close();
-      conn.close();
+      dbconn.close();
       btThread = null;
 
     } 
     catch(Exception e) 
     { 
+      System.out.println("Error in relationHandler.run: " + e.getMessage());
+      util.debugMessage("Error in relationHandler.run: " + e.getMessage());
+      e.printStackTrace(System.out);
+      btThread = null;
       try 
       { 
         conn.rollback();
       } 
       catch (SQLException sqle) {}
-      System.out.println("Error in relationHandler.run: " + e.getMessage());
-      util.debugMessage("Error in relationHandler.run: " + e.getMessage());
-      e.printStackTrace(System.out);
-      btThread = null;
     }
   }
   
@@ -260,23 +262,37 @@
    */
   public void deleteRelations(String docid)
   {
+    Connection dbconn = null;
     try
     {
-      PreparedStatement pstmt = conn.prepareStatement("delete from " +
+      dbconn = MetacatReplication.getDBConnection("RelationHandler."+
+                                                  "deleteRelations");
+    }
+    catch(Exception ee)
+    {
+      System.out.println("error in relationHandler.deleteRelations: " +
+                          ee.getMessage());
+    }
+    
+    try
+    {
+      PreparedStatement pstmt = dbconn.prepareStatement("delete from " +
                              "xml_relation where docid like '" + docid + "'");
       pstmt.execute();
       pstmt.close();
+      dbconn.close();
     }
     catch(Exception e)
     {
+      System.out.println("error in RelationHandler.deleteRelations(): " + 
+                          e.getMessage());
+      e.printStackTrace(System.out);
       try 
       { 
-        conn.rollback();
+        dbconn.rollback();
+        dbconn.close();
       } 
       catch (SQLException sqle) {}
-      System.out.println("error in RelationHandler.deleteRelations(): " + 
-                          e.getMessage());
-      e.printStackTrace(System.out);
     }
     
   }
Index: src/edu/ucsb/nceas/metacat/DocumentIdentifier.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DocumentIdentifier.java	(revision 682)
+++ src/edu/ucsb/nceas/metacat/DocumentIdentifier.java	(revision 683)
@@ -109,7 +109,31 @@
     
     try
     {
-      conn = util.openDBConnection();
+      try
+      { //this connection is prone to error for some reason so we 
+        //try to connect it three times before quiting.
+        conn = util.openDBConnection();
+      }
+      catch(SQLException sqle)
+      {
+        try
+        {
+          conn = util.openDBConnection();
+        }
+        catch(SQLException sqlee)
+        {
+          try
+          {
+            conn = util.openDBConnection();
+          }
+          catch(SQLException sqleee)
+          {
+            System.out.println("error getting db connection in " + 
+                               "MetacatReplication.handleGetDocumentRequest: " +
+                               sqleee.getMessage());
+          }
+        }
+      }
       pstmt = conn.prepareStatement("select rev from xml_documents where " +
                                     "docid like '" + docid + "'");
       pstmt.execute();
@@ -122,9 +146,12 @@
         conn.close();
         return retStr;
       }
+      conn.close();
     }
     catch(Exception e)
     {
+      System.out.println("error in DocumentIdentifier.getNewestRev(): " +
+                          e.getMessage());
       try
       {
         conn.close();
@@ -131,9 +158,6 @@
       }
       catch(Exception e2)
       {}
-      System.out.println("error closing db connection in " + 
-                         "DocumentIdentifier.getNewestRev " + 
-                          e.getMessage());
     }
     return "1";
   }
