diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-01-19 10:12:00 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-01-19 10:12:00 +0000 |
commit | bca75d353f2055dbff8cf95e35497e6162af5d73 (patch) | |
tree | 1fcd2a0f7b9ed2e5d836af92623acd30b24deb1d | |
parent | 9e8bfddf290de2406ef089e6405b20deb19ba909 (diff) | |
download | brdo-bca75d353f2055dbff8cf95e35497e6162af5d73.tar.gz brdo-bca75d353f2055dbff8cf95e35497e6162af5d73.tar.bz2 |
- Patch #360887 by Crell: more robus example of how to use transaction support in D7.
-rw-r--r-- | includes/database/database.inc | 28 |
1 files changed, 19 insertions, 9 deletions
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) { |