Talk:DB layer 2.0 delegated transactions: Difference between revisions
From MoodleDocs
David Mudrak (talk | contribs) m (Text replacement - "</code>" to "</syntaxhighlight>") |
David Mudrak (talk | contribs) m (Text replacement - "<code php>" to "<syntaxhighlight lang="php">") |
||
Line 2: | Line 2: | ||
6. Normal usage of the moodle_delegated_transaction will be: | 6. Normal usage of the moodle_delegated_transaction will be: | ||
< | <syntaxhighlight lang="php"> | ||
$transaction = $DB->start_require_transaction(); | $transaction = $DB->start_require_transaction(); | ||
// Perform some $DB stuff | // Perform some $DB stuff | ||
Line 8: | Line 8: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
7. If, for any reason, developer needs to catch exceptions when using transactions, it will be mandatory to use it in this way: | 7. If, for any reason, developer needs to catch exceptions when using transactions, it will be mandatory to use it in this way: | ||
< | <syntaxhighlight lang="php"> | ||
try { | try { | ||
$transaction = $DB->start_require_transaction(); | $transaction = $DB->start_require_transaction(); | ||
Line 18: | Line 18: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
8. In order to be able to keep some parts of code out from top transactions completely, if we know it can lead to problems, we can use: | 8. In order to be able to keep some parts of code out from top transactions completely, if we know it can lead to problems, we can use: | ||
< | <syntaxhighlight lang="php"> | ||
$DB->forbide_transaction(); // Instant check to confirm we aren't using transactions in this point. Will throw exception if transaction is found. | $DB->forbide_transaction(); // Instant check to confirm we aren't using transactions in this point. Will throw exception if transaction is found. | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 20:37, 14 July 2021
Alternate API (from comments in Tracker/HQ chat
6. Normal usage of the moodle_delegated_transaction will be:
$transaction = $DB->start_require_transaction();
// Perform some $DB stuff
$transaction->end_require_transaction();
7. If, for any reason, developer needs to catch exceptions when using transactions, it will be mandatory to use it in this way:
try {
$transaction = $DB->start_require_transaction();
// Perform some $DB stuff
$transaction->end_require_transaction();
} catch (exception $e) {
$transaction->rollback($e); // ?
}
8. In order to be able to keep some parts of code out from top transactions completely, if we know it can lead to problems, we can use:
$DB->forbide_transaction(); // Instant check to confirm we aren't using transactions in this point. Will throw exception if transaction is found.