Index: src/edu/ucsb/nceas/metacat/DocumentImpl.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 2750)
+++ src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 2751)
@@ -296,26 +296,49 @@
             String accnum, String user, String[] groups, int serverCode)
             throws SQLException, AccessionNumberException, Exception
     {
-
-        String action = null;
-        String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
-        int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
-        action = checkRevInXMLDocuments(accnum, userSpecifyRev);
-        if (action.equals("UPDATE"))
+        DBConnection conn = null;
+        int serialNumber = -1;
+        try
         {
-            //archive the old entry
-            if (!hasWritePermission(user, groups, accnum)) { throw new Exception(
-                    "User " + user
-                    + " does not have permission to update the document"
-                    + accnum); }
-            archiveDocRevision(docIdWithoutRev, user);  
+            conn = DBConnectionPool
+                .getDBConnection("DocumentImpl.registerDocumentInreplication");
+            serialNumber = conn.getCheckOutSerialNumber();
+            conn.setAutoCommit(false);
+            String action = null;
+            String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
+            int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
+            action = checkRevInXMLDocuments(docIdWithoutRev, userSpecifyRev);
+            logMetacat.warn("after check rev, the action is "+action);
+            if (action.equals("UPDATE"))
+            {
+                //archive the old entry
+                if (!hasWritePermission(user, groups, accnum)) { throw new Exception(
+                        "User " + user
+                        + " does not have permission to update the document"
+                        + accnum); }
+                archiveDocRevision(docIdWithoutRev, user, conn);  
+            }
+            
+            String rev = Integer.toString(userSpecifyRev);
+            modifyRecordsInGivenTable(DOCUMENTTABLE, action,docIdWithoutRev, doctype, docname,
+                        user, rev, serverCode, null, null, conn);
+                        // null and null is createdate and updatedate
+                        // null will create current time
+            conn.commit();
+            conn.setAutoCommit(true);
         }
+        catch(Exception e)
+        {
+            conn.rollback();
+            conn.setAutoCommit(true);
+            throw e;
+        }
+        finally
+        {
+            //check in DBConnection
+            DBConnectionPool.returnDBConnection(conn, serialNumber);
+        }
         
-        String rev = Integer.toString(userSpecifyRev);
-        modifyRecordsInGivenTable(DOCUMENTTABLE, action,docIdWithoutRev, doctype, docname,
-                    user, rev, serverCode, null, null);
-                    // null and null is createdate and updatedate
-                    // null will create current time
     }
 
     /**
@@ -352,32 +375,54 @@
             String tableName, String createDate, String updateDate)
             throws SQLException, AccessionNumberException, Exception
     {
-       
-        String action = null;
-        String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
-        int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
-        if (tableName.equals(DOCUMENTTABLE))
+        DBConnection conn = null;
+        int serialNumber = -1;
+        try
         {
-            action = checkRevInXMLDocuments(accnum, userSpecifyRev);
-            if (action.equals("UPDATE"))
+            //check out DBConnection
+            conn = DBConnectionPool
+                    .getDBConnection("DocumentImpl.registerDocumentInreplication");
+            serialNumber = conn.getCheckOutSerialNumber();
+            conn.setAutoCommit(false);
+            String action = null;
+            String docIdWithoutRev = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
+            int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
+            if (tableName.equals(DOCUMENTTABLE))
             {
-                    //archive the old entry
-                    archiveDocRevision(docIdWithoutRev, user);  
+                action = checkRevInXMLDocuments(docIdWithoutRev, userSpecifyRev);
+                if (action.equals("UPDATE"))
+                {
+                        //archive the old entry
+                        archiveDocRevision(docIdWithoutRev, user, conn);  
+                }
             }
+            else if (tableName.equals(REVISIONTABLE))
+            {
+                action = checkXMLRevisionTable(docIdWithoutRev, userSpecifyRev);
+            }
+            else
+            {
+                throw new Exception("Couldn't handle this table name "+tableName);
+            }
+           
+            String rev = Integer.toString(userSpecifyRev);
+            modifyRecordsInGivenTable(tableName, action,docIdWithoutRev, doctype, docname,
+                        user, rev, serverCode, createDate, updateDate, conn);
+            conn.commit();
+            conn.setAutoCommit(true);
         }
-        else if (tableName.equals(REVISIONTABLE))
+        catch(Exception e)
         {
-            action = checkXMLRevisionTable(accnum, userSpecifyRev);
+            conn.rollback();
+            conn.setAutoCommit(true);
+            throw e;
         }
-        else
+        finally
         {
-            throw new Exception("Couldn't handle this table name "+tableName);
+            //check in DBConnection
+            DBConnectionPool.returnDBConnection(conn, serialNumber);
         }
        
-        String rev = Integer.toString(userSpecifyRev);
-        modifyRecordsInGivenTable(tableName, action,docIdWithoutRev, doctype, docname,
-                    user, rev, serverCode, createDate, updateDate);
-       
     }
     
    /*
@@ -385,11 +430,11 @@
     */
    private static void modifyRecordsInGivenTable(String tableName, String action,
                        String docid, String doctype, String docname, String user,
-                       String rev, int serverCode, String createDate, String updateDate) throws Exception
+                       String rev, int serverCode, String createDate, String updateDate,
+                       DBConnection dbconn) throws Exception
    {
-      DBConnection dbconn = null;
+      
       PreparedStatement pstmt = null;
-      int serialNumber = -1;
       int revision = (new Integer(rev)).intValue();
       String sqlDateString = dbAdapter.getDateTimeFunction();
       if (createDate == null)
@@ -411,11 +456,10 @@
       }
       try
       {
-        dbconn = DBConnectionPool.getDBConnection(
-          "DocumentImpl.registerDocumentInReplication");
-        serialNumber = dbconn.getCheckOutSerialNumber();
+        
         StringBuffer sql = new StringBuffer();
         if (action != null && action.equals("INSERT")) {
+            
             sql.append("insert into ");
             sql.append(tableName);
             sql.append(" (docid, docname, " +
@@ -434,6 +478,7 @@
             sql.append(updateDate).append(",");
             sql.append("'0')");
         } else if (action != null && action.equals("UPDATE")) {
+            
             sql.append("update xml_documents set docname ='");
             sql.append(docname).append("', ");
             sql.append("user_updated='");
@@ -451,15 +496,19 @@
         pstmt.execute();
         pstmt.close();
        
-    } finally {
-        try {
-            if(pstmt != null){
-                pstmt.close();
-            }
-        } finally {
-            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
-        }
-    }     
+    }
+    catch(Exception e) 
+    {
+     
+        throw e;
+    }
+    finally 
+    {
+       if(pstmt != null)
+       {
+         pstmt.close();
+       }
+     }     
    }
     /**
      * This method will register a data file entry in xml_documents and save a
@@ -505,30 +554,41 @@
         // Get server code again
         serverCode = getServerCode(docHomeServer);
 
-        //register data file into xml_documents table
-        registerDocumentInReplication(docname, doctype, accnum, user,
-                serverCode, tableName, createDate, updateDate);
+        
         //write inputstream into file system.
         File dataDirectory = new File(filePath);
-        File newFile = new File(dataDirectory, accnum);
-
-        // create a buffered byte output stream
-        // that uses a default-sized output buffer
-        FileOutputStream fos = new FileOutputStream(newFile);
-        BufferedOutputStream outPut = new BufferedOutputStream(fos);
-
-        BufferedInputStream bis = null;
-        bis = new BufferedInputStream(input);
-        byte[] buf = new byte[4 * 1024]; // 4K buffer
-        int b = bis.read(buf);
-
-        while (b != -1) {
-            outPut.write(buf, 0, b);
-            b = bis.read(buf);
+        File newFile = null;
+        try
+        {
+            newFile = new File(dataDirectory, accnum);
+    
+            // create a buffered byte output stream
+            // that uses a default-sized output buffer
+            FileOutputStream fos = new FileOutputStream(newFile);
+            BufferedOutputStream outPut = new BufferedOutputStream(fos);
+    
+            BufferedInputStream bis = null;
+            bis = new BufferedInputStream(input);
+            byte[] buf = new byte[4 * 1024]; // 4K buffer
+            int b = bis.read(buf);
+    
+            while (b != -1) {
+                outPut.write(buf, 0, b);
+                b = bis.read(buf);
+            }
+            bis.close();
+            outPut.close();
+            fos.close();
+            
+            //register data file into xml_documents table
+            registerDocumentInReplication(docname, doctype, accnum, user,
+                    serverCode, tableName, createDate, updateDate);
         }
-        bis.close();
-        outPut.close();
-        fos.close();
+        catch (Exception ee)
+        {
+            newFile.delete();
+            throw ee;
+        }
         
         // Force replicate data file
         if (!timedReplication)
@@ -546,10 +606,11 @@
      private static String checkRevInXMLDocuments(String docid, int userSpecifyRev) throws Exception
      {
         String action = null;
-        logMetacat.info("The user specifyRev: " + userSpecifyRev);
+        logMetacat.warn("The docid without rev is "+docid);
+        logMetacat.warn("The user specifyRev: " + userSpecifyRev);
         // Revision for this docid in current database
         int revInDataBase =DBUtil.getLatestRevisionInDocumentTable(docid);
-        logMetacat.info("The rev in data base: " + revInDataBase);
+        logMetacat.warn("The rev in data base: " + revInDataBase);
         // String to store the revision
         String rev = null;
 
@@ -2412,12 +2473,13 @@
         logMetacat.info("user in replication" + user);
         // Docid without revision
         String docid = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
+        logMetacat.warn("The docid without rev is " + docid);
         // Revision specified by user (int)
         int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
-        logMetacat.info("The user specifyRev: " + userSpecifyRev);
+        logMetacat.warn("The user specifyRev: " + userSpecifyRev);
         // Revision for this docid in current database
         int revInDataBase = DBUtil.getLatestRevisionInDocumentTable(docid);
-        logMetacat.info("The rev in data base: " + revInDataBase);
+        logMetacat.warn("The rev in data base: " + revInDataBase);
         // String to store the revision
         String rev = (new Integer(userSpecifyRev)).toString();
 
@@ -3147,11 +3209,9 @@
     }
 
     /** Save a document entry in the xml_revisions table */
-    private static void archiveDocRevision(String docid, String user) throws Exception
+    private static void archiveDocRevision(String docid, String user, DBConnection conn) throws Exception
     {
         String sysdate = dbAdapter.getDateTimeFunction();
-        DBConnection conn = null;
-        int serialNumber = -1;
         PreparedStatement pstmt = null;
 
         // create a record in xml_revisions table
@@ -3159,9 +3219,6 @@
 
         try {
             //check out DBConnection
-            conn = DBConnectionPool
-                    .getDBConnection("DocumentImpl.archiveDocRevision");
-            serialNumber = conn.getCheckOutSerialNumber();
             pstmt = conn.prepareStatement("INSERT INTO xml_revisions "
                     + "(docid, rootnodeid, docname, doctype, "
                     + "user_owner, user_updated, date_created, date_updated, "
@@ -3190,10 +3247,7 @@
                         "Error in DocumnetImpl.archiveDocRevision: "
                                 + ee.getMessage());
                 throw ee;
-            } finally {
-                //check in DBConnection
-                DBConnectionPool.returnDBConnection(conn, serialNumber);
-            }
+            } 
         }
     }
 
