Transactions should be allowed to be committed explicitly

Created on 13 January 2011, almost 14 years ago
Updated 5 December 2023, about 1 year ago

This is a fork from this issue. Based on the snippet from Database abstraction layer shown below, a commit will be done automatically when $txn goes out of scope.

function my_transaction_function() {
  // The transaction opens here.
  $txn = db_transaction();

  .
  .
  .

  // $txn goes out of scope here.  Unless the transaction was rolled back, it
  // gets automatically commited here.
}

It doesn't feel quite right to have commit as the default action because:

  • Quote from PHP:
    When the script ends or when a connection is about to be closed, if you have an outstanding transaction, PDO will automatically roll it back. This is a safety measure to help avoid inconsistency in the cases where the script terminates unexpectedly--if you didn't explicitly commit the transaction, then it is assumed that something went awry, so the rollback is performed for the safety of your data.
    The common practice for a database transaction, whether is MySQL or pgSQL, is to do a default rollback when there is no explicit commit action given. This ensures that for whatever bad happens, inconsistent data are not written. In other words, the coder is sure the data at that point in time is consistent and therefore codes an explicit commit() action there. If something goes wrong, the commit() code will not be executed and rollback should happen.
  • Quote from PHP:
    The destructor will be called even if script execution is stopped using exit().
    Therefore, when exit(), drupal_exit() or drupal_goto() is executed, it is as good as doing a commit (tested and true). This might not be the real intention of the coder.
๐Ÿ“Œ Task
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Databaseย  โ†’

Last updated 2 days ago

  • Maintained by
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands @daffie
Created by

๐Ÿ‡ธ๐Ÿ‡ฌSingapore turboflash

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024