Index: trunk/ippTools/src/stacktool.c
===================================================================
--- trunk/ippTools/src/stacktool.c	(revision 18976)
+++ trunk/ippTools/src/stacktool.c	(revision 19029)
@@ -767,28 +767,96 @@
     PXOPT_COPY_S16(config->args, where, "-code", "fault", "==");
 
-    psString query = pxDataGet("stacktool_revertsumskyfile.sql");
-    if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        return false;
-    }
-
-    if (psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(where, "stackSumSkyfile");
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(where);
+        return false;
+    }
+
+    int numStacks;                      // Number of stacks affected
+
+    // Update state to 'run'
+    {
+        psString update = pxDataGet("stacktool_revertsumskyfile_update.sql");
+        if (!update) {
+            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            return false;
+        }
+
+        if (psListLength(where->list)) {
+            psString whereClause = psDBGenerateWhereConditionSQL(where, "stackSumSkyfile");
+            psStringAppend(&update, " AND %s", whereClause);
+            psFree(whereClause);
+        }
+
+        if (!p_psDBRunQuery(config->dbh, update)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            psFree(update);
+            psFree(where);
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            return false;
+        }
+        psFree(update);
+
+        numStacks = psDBAffectedRows(config->dbh);
+
+        if (numStacks < 1) {
+            psError(PS_ERR_UNKNOWN, false, "should have affected atleast 1 row");
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            return false;
+        }
+    }
+
+    // Delete product
+    {
+        psString delete = pxDataGet("stacktool_revertsumskyfile_delete.sql");
+        if (!delete) {
+            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            return false;
+        }
+
+        if (psListLength(where->list)) {
+            psString whereClause = psDBGenerateWhereConditionSQL(where, "stackSumSkyfile");
+            psStringAppend(&delete, " AND %s", whereClause);
+            psFree(whereClause);
+        }
+
+        if (!p_psDBRunQuery(config->dbh, delete)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            psFree(delete);
+            psFree(where);
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            return false;
+        }
+        psFree(delete);
+
+        if (psDBAffectedRows(config->dbh) != numStacks) {
+            psError(PS_ERR_UNKNOWN, true, "Updated and deleted different number of entries!");
+            psFree(where);
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            return false;
+        }
+
+    }
+
+    if (!psDBCommit(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
     psFree(where);
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-    psFree(query);
-
-    if (psDBAffectedRows(config->dbh) < 1) {
-        psError(PS_ERR_UNKNOWN, false, "should have affected atleast 1 row");
-        return false;
-    }
 
     return true;
