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>" to "</syntaxhighlight>")
Line 6: Line 6:
// 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>
<code php>
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>
<code 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>

Revision as of 20:19, 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(); </syntaxhighlight> 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); // ?

} </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:

$DB->forbide_transaction(); // Instant check to confirm we aren't using transactions in this point. Will throw exception if transaction is found.

</syntaxhighlight>