Transactions
The Drupal database API also provides a way to represent and handle database transactions (for database types that support them). Transactions are a way in which database operations can be wrapped and grouped together with a view of committing them in an “all or none” type of fashion. For example, if you have multiple records that are related, it’s possible you will not want them written if one fails its INSERT
operation for some reason. It could leave you with corrupt or incomplete data that could throw your application into a spin.
Performing multiple database-changing operations after a transaction has been opened only finalizes (commits) those changes to the database when that transaction closes. If something goes wrong, it can also be rolled back, which will prevent the data from being committed.
In Drupal, a transaction is represented by a Transaction
object (a specific subclass for each database type). As soon as the object is destroyed...