8.5.x database.inc db_transaction($name = NULL, array $options = [])
8.0.x database.inc db_transaction($name = NULL, array $options = array())
8.1.x database.inc db_transaction($name = NULL, array $options = array())
8.2.x database.inc db_transaction($name = NULL, array $options = array())
8.3.x database.inc db_transaction($name = NULL, array $options = [])
8.4.x database.inc db_transaction($name = NULL, array $options = [])
8.6.x database.inc db_transaction($name = NULL, array $options = [])
7.x database.inc db_transaction($name = NULL, array $options = array())

Returns a new transaction object for the active database.

Parameters

string $name: Optional name of the transaction.

array $options: An array of options to control how the transaction operates:

  • target: The database target name.

Return value

DatabaseTransaction A new DatabaseTransaction object for this connection.

Related topics

17 calls to db_transaction()
block_admin_configure_submit in modules/block/block.admin.inc
Form submission handler for block_admin_configure().
block_admin_display_form_submit in modules/block/block.admin.inc
Form submission handler for block_admin_display_form().
comment_delete_multiple in modules/comment/comment.module
Delete comments and all their replies.
comment_save in modules/comment/comment.module
Accepts a submission of new or changed comment content.
DatabaseTransactionTestCase::testTransactionStacking in modules/simpletest/tests/database_test.test
Test transaction stacking and commit / rollback.

... See full list

File

includes/database/database.inc, line 2594
Core systems for the database layer.

Code

function db_transaction($name = NULL, array $options = array()) {
  if (empty($options['target'])) {
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])
    ->startTransaction($name);
}

Comments

rszrama’s picture

The first argument will not be used in the case of the first transaction for the database connection; the first one will always be set to drupal_transaction regardless of what you specify.

iuer’s picture

Some explications about "array $options" ?
Is there only "target" inside?

jrbeeman’s picture

Documentation on using db_transaction() can be found in the Database API section of the handbook:
http://drupal.org/node/355875

Liam Morland’s picture

You must assign the return value of db_transaction() to a variable, as in the example. If you call the function without assigning the return to a variable, your transaction will commit instantly, making it useless.