Index: src/edu/ucsb/nceas/metacat/DBSAXHandler.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DBSAXHandler.java	(revision 1840)
+++ src/edu/ucsb/nceas/metacat/DBSAXHandler.java	(revision 1841)
@@ -400,13 +400,54 @@
     int step = 0;
     int counter = 0;
 
-    try {
-
+    try 
+    {
+      // stop 5 second
+      Thread.sleep(5000);
       // Opening separate db connection for writing XML Index
       dbConn=DBConnectionPool.getDBConnection("DBSAXHandler.run");
       serialNumber=dbConn.getCheckOutSerialNumber();
       dbConn.setAutoCommit(false);
+      //make sure record is done
+      checkDocumentTable();   
+      // Going through the elements of the document and writing its Index
+      Enumeration nodes = nodeIndex.elements();
+      while ( nodes.hasMoreElements() ) {
+        currNode = (DBSAXNode)nodes.nextElement();
+        currNode.updateNodeIndex(dbConn, docid, doctype);
+      }
+      dbConn.commit();
 
+    } catch (Exception e) {
+      try {
+        dbConn.rollback();
+        //dbconn.close();
+      } catch (SQLException sqle) {}
+      MetaCatUtil.debugMessage("Error in DBSAXHandler.run " + 
+                                e.getMessage(), 30);
+      
+    }
+    finally
+    {
+      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
+    }//finally
+  }
+
+  /* method to make sure insert is finished before create index table
+   * If new version of record is in xml_documents every thing will be fine
+   */
+  private void checkDocumentTable() throws Exception
+  {
+    
+    DBConnection dbConn = null;
+    int serialNumber = -1;
+   
+    try 
+    {
+       // Opening separate db connection for writing XML Index
+      dbConn=DBConnectionPool.getDBConnection("DBSAXHandler.checkDocumentTable");
+      serialNumber=dbConn.getCheckOutSerialNumber();
+     
       //the following while loop construct checks to make sure that the docid
       //of the document that we are trying to index is already
       //in the xml_documents table.  if this is not the case, the foreign
@@ -416,7 +457,10 @@
       long startTime = System.currentTimeMillis();
       while(!inxmldoc)
       {
-        String xmlDocumentsCheck = "select distinct docid from xml_documents";
+        String xmlDocumentsCheck = "select distinct docid from xml_documents" +
+                                   " where docid ='" + docid + "' and " +
+                                   " rev ='" + revision + "'";
+       
         PreparedStatement xmlDocCheck =
                                  dbConn.prepareStatement(xmlDocumentsCheck);
         // Increase usage count
@@ -424,51 +468,31 @@
         xmlDocCheck.execute();
         ResultSet doccheckRS = xmlDocCheck.getResultSet();
         boolean tableHasRows = doccheckRS.next();
-        Vector docids = new Vector();
-        while(tableHasRows)
+        if (tableHasRows)
         {
-          docids.add(doccheckRS.getString(1).trim());
-          tableHasRows = doccheckRS.next();
+           MetaCatUtil.debugMessage("=========== find the correct document", 35);
+           inxmldoc = true;
         }
-
-        for(int i=0; i<docids.size(); i++)
-        {
-          String d = ((String)docids.elementAt(i)).trim();
-          if(docid.trim().equals(d))
-          {
-            inxmldoc = true;
-          }
-        }
-        doccheckRS.close();
-        xmlDocCheck.close();
-        // make sure the while loop will be ended in reseaonable time
-        long stopTime = System.currentTimeMillis();
-        if ((stopTime - startTime) > INDEXDELAY)
-        {
-          throw new Exception("Couldn't find the docid for index build in" +
+         doccheckRS.close();
+         xmlDocCheck.close();
+         // make sure the while loop will be ended in reseaonable time
+         long stopTime = System.currentTimeMillis();
+         if ((stopTime - startTime) > INDEXDELAY)
+         {
+           throw new Exception("Couldn't find the docid for index build in" +
                               "reseaonable time!");
-        }
-      }
-
-      // Going through the elements of the document and writing its Index
-      Enumeration nodes = nodeIndex.elements();
-      while ( nodes.hasMoreElements() ) {
-        currNode = (DBSAXNode)nodes.nextElement();
-        currNode.updateNodeIndex(dbConn, docid, doctype);
-      }
-
-
-      dbConn.commit();
-
-      //if this is a package file
-    
-   
-
-    } catch (Exception e) {
-      try {
+         }
+       }//while
+    } 
+    catch (Exception e) 
+    {
+      try 
+      {
         dbConn.rollback();
         //dbconn.close();
-      } catch (SQLException sqle) {}
+      } 
+      catch (SQLException sqle) 
+      {}
       MetaCatUtil.debugMessage("Error in DBSAXHandler.run " + 
                                 e.getMessage(), 30);
       
@@ -477,8 +501,9 @@
     {
       DBConnectionPool.returnDBConnection(dbConn, serialNumber);
     }//finally
+  
   }
-
+  
   /** SAX Handler that is called for each XML text node */
   public void characters(char[] cbuf, int start, int len) throws SAXException 
   {
