I can't understand a specific part of the Drupal code, namely this snippet:

  $account->uid = db_next_id(db_query('SELECT MAX(uid) FROM {users}')->fetchField());

  // Allow 'created' to be set by the caller.
  if (!isset($account->created)) {
    $account->created = REQUEST_TIME;

  $success = drupal_write_record('users', $account);

This is from the Drupal 7 code

My question is about concurrency. I imagined that two concurrent sessions are executing this and because the lack of transaction handling here they might try to insert two records with the same uid.

Can you tell me why this works in production? How is it possible that Drupal uses no transaction management although for example creating a node usually involves writing records into many different tables.

1 Answer 1


How is it possible that Drupal uses no transaction management?

It's not. The code you're querying is part of user_save(); the very first line in that function is:

$transaction = db_transaction();

The rest of the function is wrapped in a try...catch that explicitly rolls back the transaction upon exception. The same is true for node_save(), and plenty of other core functions that require transactions.

See the db_transaction() docs for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.