Add transaction() methods to MysqlClient#216
Open
jdickinsondev91-stack wants to merge 2 commits intofriends-of-reactphp:0.7.xfrom
Open
Add transaction() methods to MysqlClient#216jdickinsondev91-stack wants to merge 2 commits intofriends-of-reactphp:0.7.xfrom
jdickinsondev91-stack wants to merge 2 commits intofriends-of-reactphp:0.7.xfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add transaction() convenience method to MysqlClient
Currently, every application that needs atomic operations must manually issue START TRANSACTION, COMMIT, and ROLLBACK as raw queries and handle the rollback logic in a try/catch:
$mysql->query('START TRANSACTION');
try {
$mysql->query('INSERT INTO user (name) VALUES (?)', ['Alice']);
$mysql->query('INSERT INTO user (name) VALUES (?)', ['Bob']);
$mysql->query('COMMIT');
} catch (\Throwable $e) {
$mysql->query('ROLLBACK');
throw $e;
}
This PR adds a transaction() method that handles this automatically:
$mysql->transaction(function (React\Mysql\MysqlClient $mysql) {
$mysql->query('INSERT INTO user (name) VALUES (?)', ['Alice']);
$mysql->query('INSERT INTO user (name) VALUES (?)', ['Bob']);
});
The method:
This pattern is standard in other database libraries (DB::transaction() in Laravel, transactional() in Doctrine, BEGIN/COMMIT wrappers in node-postgres and Go's database/sql).
Changes: