Index: trunk/ippTools/src/pstamptool.c
===================================================================
--- trunk/ippTools/src/pstamptool.c	(revision 26557)
+++ trunk/ippTools/src/pstamptool.c	(revision 26565)
@@ -925,4 +925,15 @@
     PXOPT_LOOKUP_BOOL(no_create,  config->args, "-no_create", false);
 
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
+    PXOPT_COPY_STR(config->args, where, "-imagedb", "imagedb", "==");
+    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
+
+    // start a transaction eraly so it will contain any row level locks
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
     psString query = pxDataGet("pstamptool_pendingdependent.sql");
     if (!query) {
@@ -930,4 +941,14 @@
         return false;
     }
+    psString whereClause = psDBGenerateWhereSQL(where, "WHERE");
+    if (!no_create) {
+        // This will lock the row until the transaction is committed
+        psStringAppend(&query, " %s FOR UPDATE", whereClause);
+    } else {
+        psStringAppend(&query, " %s", whereClause);
+    }
+    psFree(whereClause);
+    psFree(where);
+
     if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -947,13 +968,24 @@
         if (!dep_id) {
             psError(PS_ERR_UNKNOWN, false, "database error");
+            psFree(output);
             return false;
         }
         printf("%" PRId64 "\n", dep_id);
+        psFree(output);
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
         return true;
     }
     if (no_create) {
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
         return true;
     }
     // no existing dependent that matches, insert one
+    // Since we have multiple processes running jobs we have a 
+    // race condition here so that's why we need to lock the table
 
     if (!pstampDependentInsert(
@@ -967,10 +999,21 @@
         no_magic
         )) {
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
         psError(PS_ERR_UNKNOWN, false, "failed to insert pstampDependent");
         return false;
     }
 
+    if (!psDBCommit(config->dbh)) {
+        // rollback
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
     psS64 dep_id = psDBLastInsertID(config->dbh);
-
     printf("%" PRId64 "\n", dep_id);
 
