From a5f23317ef61276828386d90bd7787580b5b9ea9 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Mon, 19 Jul 2010 13:28:19 +0000 Subject: - Patch #851110 by Damien Tournoud: InsertQuery::execute() needs to rollback the transaction if one insert fail. --- includes/database/query.inc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'includes') diff --git a/includes/database/query.inc b/includes/database/query.inc index 9e033304e..55c569e00 100644 --- a/includes/database/query.inc +++ b/includes/database/query.inc @@ -486,9 +486,18 @@ class InsertQuery extends Query { // we wrap it in a transaction so that it is atomic where possible. On many // databases, such as SQLite, this is also a notable performance boost. $transaction = $this->connection->startTransaction(); - $sql = (string) $this; - foreach ($this->insertValues as $insert_values) { - $last_insert_id = $this->connection->query($sql, $insert_values, $this->queryOptions); + + try { + $sql = (string) $this; + foreach ($this->insertValues as $insert_values) { + $last_insert_id = $this->connection->query($sql, $insert_values, $this->queryOptions); + } + } + catch (Exception $e) { + // One of the INSERTs failed, rollback the whole batch. + $transaction->rollback(); + // Rethrow the exception for the calling code. + throw $e; } // Re-initialize the values array so that we can re-use this query. -- cgit v1.2.3