Merge pull request #7038 from nextcloud/backport/7037/stable22

[stable22] Document correct db transaction handling
This commit is contained in:
Marcel Klehr 2021-08-13 13:58:36 +02:00 committed by GitHub
commit 07c6663343
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -45,6 +45,33 @@ Inside your database layer class you can now start running queries like:
}
Transactions
------------
Database operations can be run in a transaction to commit or roll back a group of changes in an atomic fashion.
.. code-block:: php
<?php
$this->db->startTransaction();
try {
// DB operations
$this->db->commit();
} catch (\Throwable $e) {
// Optional: handle the error
// Important: roll back (or commit) your changes when an error
// happens, so this transaction ends
$this->db->rollBack();
throw $e;
}
.. warning:: Omitting the error handling for transactions will lead to unexpected behavior as any database operations that come after your error will still run in your transaction and due to the lack of a commit PDO will automatically roll-back all changes at the end of the script.
Mappers
-------