From bca75d353f2055dbff8cf95e35497e6162af5d73 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Mon, 19 Jan 2009 10:12:00 +0000 Subject: - Patch #360887 by Crell: more robus example of how to use transaction support in D7. --- includes/database/database.inc | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'includes/database') diff --git a/includes/database/database.inc b/includes/database/database.inc index b29cd35ae..5a40817c4 100644 --- a/includes/database/database.inc +++ b/includes/database/database.inc @@ -134,17 +134,27 @@ * // The transaction opens here. * $txn = db_transaction(); * - * $id = db_insert('example') - * ->fields(array( - * 'field1' => 'mystring', - * 'field2' => 5, - * )) - * ->execute(); + * try { + * $id = db_insert('example') + * ->fields(array( + * 'field1' => 'mystring', + * 'field2' => 5, + * )) + * ->execute(); + * + * my_other_function($id); * - * my_other_function($id); + * return $id; + * } + * catch (Exception $e) { + * // Something went wrong somewhere, so flag the entire transaction to + * // roll back instead of getting committed. It doesn't actually roll back + * // yet, just gets flagged to do so. + * $txn->rollback(); + * } * - * return $id; - * // $txn goes out of scope here, and the entire transaction commits. + * // $txn goes out of scope here. If there was a problem, it rolls back + * // automatically. If not, it commits automatically. * } * * function my_other_function($id) { -- cgit v1.2.3