<?php
function my_transaction_function() {
// The transaction opens here.
$transaction = db_transaction();
try {
$id = db_insert('example')
->fields(array(
'field1' => 'mystring',
'field2' => 5,
))
->execute();
my_other_function($id);
return $id;
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('my_type', $e);
}
// $transaction goes out of scope here. Unless the it was rolled back, it
// gets automatically commited here.
}
function my_other_function($id) {
// The transaction is still open here.
if ($id % 2 == 0) {
db_update('example')
->condition('id', $id)
->fields(array('field2' => 10))
->execute();
}
}
?>