Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
39ee632
Pass CommandInterface to retry handler (fixes #1155, #1130)
bautrukevich Mar 29, 2026
2c6522b
Enhance internalExecute to support automatic connection renewal on fi…
bautrukevich Mar 29, 2026
ee253e3
Apply PHP CS Fixer and Rector changes (CI)
bautrukevich Mar 29, 2026
a8c1cc9
Update AbstractPdoCommand.php to support retries with bindParam storage
bautrukevich Mar 31, 2026
83d7d99
Apply suggestions from code review
samdark Apr 1, 2026
76e66cc
Enhance internalExecute to support automatic connection renewal on fi…
bautrukevich Apr 2, 2026
57f83d1
Apply PHP CS Fixer and Rector changes (CI)
bautrukevich Apr 2, 2026
a4653a5
Add psalm type hint for retry handler in AbstractCommand and CommandI…
bautrukevich Apr 2, 2026
91d570b
Clarify documentation for retry handler closure in AbstractCommand an…
bautrukevich Apr 2, 2026
9f4ca1e
Merge remote-tracking branch 'origin/feature/pass-command-to-retry-ha…
bautrukevich Apr 2, 2026
0f64f53
Update src/Command/AbstractCommand.php
bautrukevich Apr 3, 2026
08fd065
Remove rebindBoundParams method and update documentation for paramete…
bautrukevich Apr 3, 2026
67c2e8d
Implement connection recovery handler and enhance error handling in A…
bautrukevich Apr 3, 2026
8f6f17a
Update src/Driver/Pdo/AbstractPdoCommand.php
bautrukevich Apr 3, 2026
79f449d
Apply PHP CS Fixer and Rector changes (CI)
bautrukevich Apr 3, 2026
ffb39dd
Remove isConnectionError method from AbstractPdoCommand
bautrukevich Apr 3, 2026
c0811d7
Refactor parameter binding logic in AbstractPdoCommand
bautrukevich Apr 3, 2026
836a751
Add tests for parameter binding behavior across executions and after …
bautrukevich Apr 3, 2026
1d38847
Add ConnectionException and ConnectionRecoveryHandler classes; enhanc…
bautrukevich Apr 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,30 @@ public function setSql(string $sql): static
return $this;
}

/**
* Sets a closure (anonymous function) which called when a database exception is thrown when executing the command.
Comment thread
samdark marked this conversation as resolved.
Outdated
*
* The signature of the closure should be:
*
* ```php
* use Yiisoft\Db\Exception\Exception;
* use Yiisoft\Db\Command\CommandInterface;
*
* function (Exception $e, int $attempt, CommandInterface $command): bool
* {
* // return true or false (whether to retry the command or throw $e)
* }
* ```
*
* The closure will receive an {@see Exception} converted from the thrown database exception,
* the current attempt to execute the command (starting from `0`), and the {@see CommandInterface}
* instance to allow access to connection and parameters.
Comment thread
samdark marked this conversation as resolved.
Outdated
*
* If the closure returns `true`, the command will be retried. If the closure returns `false`,
* the {@see Exception} will be thrown.
*
* @param Closure|null $handler A PHP callback to handle database exceptions.
Comment thread
Tigrov marked this conversation as resolved.
Outdated
*/
public function setRetryHandler(?Closure $handler): static
{
$this->retryHandler = $handler;
Expand Down
12 changes: 7 additions & 5 deletions src/Command/CommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -776,18 +776,20 @@ public function setRawSql(string $sql): static;
*
* ```php
* use Yiisoft\Db\Exception\Exception;
* use Yiisoft\Db\Command\CommandInterface;
*
* function (Exception $e, int $attempt): bool
* function (Exception $e, int $attempt, CommandInterface $command): bool
* {
* // return true or false (whether to retry the command or throw $e)
* }
* ```
*
* The closure will receive an {@see Exception} converted from the thrown database exception and the current attempt
* to execute the command, starting from `1`.
* The closure will receive an {@see Exception} converted from the thrown database exception,
* the current attempt to execute the command (starting from `0`), and the {@see CommandInterface}
* instance to allow access to connection and parameters for reconnection logic.
Comment thread
samdark marked this conversation as resolved.
Outdated
*
* If the closure returns `true`, the command will be retried. If the closure returns `false`, the {@see Exception}
* will be thrown.
* If the closure returns `true`, the command will be retried. If the closure returns `false`,
* the {@see Exception} will be thrown.
*
* @param Closure|null $handler A PHP callback to handle database exceptions.
Comment thread
Tigrov marked this conversation as resolved.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Driver/Pdo/AbstractPdoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ protected function internalExecute(): void
$rawSql ??= $this->getRawSql();
$e = (new ConvertException($e, $rawSql))->run();

if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
// ✨ Pass $this (CommandInterface) to retry handler
Comment thread
samdark marked this conversation as resolved.
Outdated
if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt, $this)) {
throw $e;
}
Comment thread
samdark marked this conversation as resolved.
Outdated
}
Expand Down
Loading