Index: src/edu/ucsb/nceas/metacat/DocumentImpl.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 2645)
+++ src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 2646)
@@ -1950,7 +1950,7 @@
                 // Save the old document publicaccessentry in a backup table
                 String accNumber = docid+MetaCatUtil.getOption("accNumSeparator")+thisrev;
                 thisdoc = new DocumentImpl(accNumber, false);
-                DocumentImpl.archiveDocRevision(connection, docid, user, thisdoc);
+                DocumentImpl.archiveDocAndNodesRevision(connection, docid, user, thisdoc);
                 MetaCatUtil.debugMessage("after archiveDoc", 30);
                 //if the updated vesion is not greater than current one,
                 //throw it into a exception
@@ -2019,7 +2019,7 @@
             
             pstmt.close();
             if(action.equals("UPDATE")){
-                // Delete the old nodes in xml_nodes table...
+                /*// Delete the old nodes in xml_nodes table...
                 pstmt = connection.prepareStatement("DELETE FROM xml_nodes "
                                                 + "WHERE rootnodeid = ?");
                 // Increase dbconnection usage count
@@ -2027,7 +2027,8 @@
                 // Bind the values to the query and execute it
                 pstmt.setLong(1, thisdoc.getRootNodeID());
                 pstmt.execute();
-                pstmt.close();
+                pstmt.close();*/
+                deleteXMLNodes(connection, thisdoc.getRootNodeID());
             }
 
         } catch (SQLException sqle) {
@@ -2389,8 +2390,9 @@
 
         // insert into xml_nodes table
         XMLReader parser = null;
+        boolean isRevision = false;
         try {
-            boolean isRevision = false;
+            
             if (tableName.equals(REVISIONTABLE))
             {
                 isRevision = true;
@@ -2432,8 +2434,8 @@
 
         // run write into access db base on relation table and access rule
         try {
-        	conn.setAutoCommit(false);
-            if (!tableName.equals(REVISIONTABLE))
+            conn.setAutoCommit(false);
+            if (!isRevision)
             {
                runRelationAndAccessHandler(accnum, user, groups, serverCode);
             }
@@ -2443,9 +2445,13 @@
               // we need to move nodes from xml_nodes to xml_nodes_revision and register the record
             	// into xml_revision table
                moveNodesToNodesRevision(conn, rootId);
+               deleteXMLNodes(conn, rootId);
                writeDocumentToRevisionTable(conn, docid, rev, docType, docName, user, 
                        catalogId, serverCode, rootId, createDate, updateDate);
+              
             }
+            conn.commit();
+            conn.setAutoCommit(true);
             
         } catch (Exception ee) {
         	conn.rollback();
@@ -2454,7 +2460,7 @@
             {
                 // because we couldn't register the table into xml_revsion
                 // we need to delete the nodes in xml_ndoes.
-                deleteXMLNodes(rootId);
+                deleteXMLNodes(conn, rootId);
             }
             MetacatReplication.replErrorLog("Failed to " + "create access "
                     + "rule for package: " + accnum + " because "
@@ -2655,7 +2661,7 @@
 
             conn.setAutoCommit(false);
             // Copy the record to the xml_revisions table
-            DocumentImpl.archiveDocRevision(conn, docid, user, null);
+            DocumentImpl.archiveDocAndNodesRevision(conn, docid, user, null);
 
             // Now delete it from the xml_index table
             boolean useXMLIndex = (new Boolean(MetaCatUtil
@@ -2959,10 +2965,11 @@
     }
 
     /**
-     * Save a document entry in the xml_revisions table Connection use as a
+     * Save a document entry in the xml_revisions and xml_nodes_revision
+     *  table Connection use as a
      * paramter is in order to rollback feature
      */
-    private static void archiveDocRevision(DBConnection dbconn, String docid,
+    private static void archiveDocAndNodesRevision(DBConnection dbconn, String docid,
             String user, DocumentImpl doc)
     {
         
@@ -3055,6 +3062,8 @@
     private static void moveNodesToNodesRevision(DBConnection dbconn,
                                        long rootNodeId) throws Exception
     {
+        MetaCatUtil.debugMessage("the root node id is "+rootNodeId+
+                " will be moved from xml_nodes to xml_node_revision table", 20);
         PreparedStatement pstmt = null;
         // Move the nodes from xml_nodes to xml_revisions table...
         pstmt = dbconn.prepareStatement("INSERT INTO xml_nodes_revisions "
@@ -3069,6 +3078,7 @@
         pstmt.setLong(1, rootNodeId);
         pstmt.execute();
         pstmt.close();
+        
 
     }
 
@@ -3525,6 +3535,7 @@
         
         try 
         {
+            
             if (createDate == null)
             {
                 createDate = dbAdapter.getDateTimeFunction();
@@ -3533,6 +3544,7 @@
             {
                 createDate = dbAdapter.toDate(createDate, "YYYY-MM-DD HH24:MI:SS");
             }
+            MetaCatUtil.debugMessage("the create date is "+createDate, 30);
             if (updateDate == null)
             {
                 updateDate = dbAdapter.getDateTimeFunction();
@@ -3541,8 +3553,10 @@
             {
                 updateDate = dbAdapter.toDate(updateDate, "YYYY-MM-DD HH24:MI:SS");
             }
+            MetaCatUtil.debugMessage("the update date is "+updateDate, 30);
             PreparedStatement pstmt = null;
             String sql = null;
+            MetaCatUtil.debugMessage("the root node id is "+rootNodeId, 20);
             if (rootNodeId <= 0)
             {
             	// this is for data file, not rootnodeid need
@@ -3568,23 +3582,32 @@
 
             // Bind the values to the query
             pstmt.setString(1, docId);
+            MetaCatUtil.debugMessage("docid is "+docId, 50);
             pstmt.setString(2, docName);
+            MetaCatUtil.debugMessage("docname is "+docName, 50);
             pstmt.setString(3, docType);
+            MetaCatUtil.debugMessage("docType is "+docType, 50);
             pstmt.setString(4, user);
+            MetaCatUtil.debugMessage("onwer is "+user, 50);
             pstmt.setString(5, user);
+            MetaCatUtil.debugMessage("update user is "+user, 50);
             pstmt.setInt(6, 0);
             pstmt.setInt(7, (new Integer(catalogid)).intValue());
+            MetaCatUtil.debugMessage("catalogid is "+catalogid, 50);
             pstmt.setInt(8, serverCode);
+            MetaCatUtil.debugMessage("server code is "+serverCode, 50);
             pstmt.setInt(9, Integer.parseInt(rev));
+            MetaCatUtil.debugMessage("rev is "+rev, 50);
             
             if (rootNodeId >0 )
             {
               pstmt.setLong(10, rootNodeId);
+              MetaCatUtil.debugMessage("root id is "+rootNodeId, 50);
             }
             // Do the insertion
             pstmt.execute();
             pstmt.close();
-
+            MetaCatUtil.debugMessage("end of write into revisons", 50);
            
         } 
         catch (SQLException sqle) 
@@ -3594,6 +3617,7 @@
         } 
         catch (Exception e) 
         {
+            e.printStackTrace();
             throw e;
         }
     }
@@ -3643,30 +3667,17 @@
      * if in the second step some error happend, we need to delete the
      * node in xml_nodes table as roll back
      */
-    static private void deleteXMLNodes(long rootId) throws Exception
+    static private void deleteXMLNodes(DBConnection dbconn, long rootId) throws Exception
     {
-        DBConnection dbconn = null;
-        int serialNumber = -1;
         //MetaCatUtil util = new MetaCatUtil();
         AccessionNumber ac;
         PreparedStatement pstmt = null;
-        String sql = "DELETE FROM xml_documents WHERE docid = "+ rootId;
-        try 
-        {
-            //dbconn = util.openDBConnection();
-            dbconn = DBConnectionPool.getDBConnection(
-                    "DeletedDocumentImpl.deletXMLNode");
-            serialNumber = dbconn.getCheckOutSerialNumber();
-            pstmt = dbconn.prepareStatement(sql);
-            // Increase dbconnection usage count
-            dbconn.increaseUsageCount(1);
-            pstmt.close();
-            dbconn.close();
-        } 
-        finally 
-        {
-            
-            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
-        }
+        String sql = "DELETE FROM xml_nodes WHERE rootnodeid ="+ rootId;
+        pstmt = dbconn.prepareStatement(sql);
+        // Increase dbconnection usage count
+        dbconn.increaseUsageCount(1);
+        pstmt.execute();
+        pstmt.close(); 
+     
     }
 }
