Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Talk:DB layer 2.0 delegated transactions: Difference between revisions

From MoodleDocs
m (New page: === Alternate API (from comments in Tracker/HQ chat === 6. Normal usage of the moodle_delegated_transaction will be: <code php> $transaction = $DB->start_require_transaction(); // Perform...)
 
m (Text replacement - "<code php>" to "<syntaxhighlight lang="php">")
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:


6. Normal usage of the moodle_delegated_transaction will be:
6. Normal usage of the moodle_delegated_transaction will be:
<code php>
<syntaxhighlight lang="php">
$transaction = $DB->start_require_transaction();
$transaction = $DB->start_require_transaction();
// Perform some $DB stuff
// Perform some $DB stuff
$transaction->end_require_transaction();
$transaction->end_require_transaction();
</code>
</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:
<code php>
<syntaxhighlight lang="php">
try {
try {
     $transaction = $DB->start_require_transaction();
     $transaction = $DB->start_require_transaction();
Line 16: Line 16:
     $transaction->rollback($e); // ?
     $transaction->rollback($e); // ?
}
}
</code>
</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:
<code php>
<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.
</code>
</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.