12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- declare(strict_types=1);
- namespace OCP\AppFramework\Db;
- use OC\DB\Exceptions\DbalException;
- use OCP\DB\Exception;
- use OCP\IDBConnection;
- use Throwable;
- use function OCP\Log\logger;
- trait TTransactional {
-
- protected function atomic(callable $fn, IDBConnection $db) {
- $db->beginTransaction();
- try {
- $result = $fn();
- $db->commit();
- return $result;
- } catch (Throwable $e) {
- $db->rollBack();
- throw $e;
- }
- }
-
- protected function atomicRetry(callable $fn, IDBConnection $db, int $maxRetries = 3): mixed {
- for ($i = 0; $i < $maxRetries; $i++) {
- try {
- return $this->atomic($fn, $db);
- } catch (DbalException $e) {
- if (!$e->isRetryable() || $i === ($maxRetries - 1)) {
- throw $e;
- }
- logger('core')->warning('Retrying operation after retryable exception.', [ 'exception' => $e ]);
- }
- }
- }
- }
|