-
Notifications
You must be signed in to change notification settings - Fork 0
Add transaction support via sessions #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Transactions | ||
|
|
||
| A session groups several operations into a single transaction: either every write is applied, or none of them are. The API mirrors `mongodb/mongodb`, so code written against `startSession`, `startTransaction`, and `commitTransaction` keeps working unchanged. | ||
|
|
||
| Because a [client](connection.md) owns exactly one PostgreSQL connection, a transaction wraps a plain `BEGIN` / `COMMIT` / `ROLLBACK` on that connection. | ||
|
|
||
| ## Running a transaction | ||
|
|
||
| Start a session on the client, open a transaction, and commit it once every operation succeeded: | ||
|
|
||
| ```php | ||
| $session = $client->startSession(); | ||
| $session->startTransaction(); | ||
|
|
||
| try { | ||
| $accounts->updateOne(['_id' => 'alice'], ['$inc' => ['balance' => -100]], ['session' => $session]); | ||
| $accounts->updateOne(['_id' => 'bob'], ['$inc' => ['balance' => 100]], ['session' => $session]); | ||
|
|
||
| $session->commitTransaction(); | ||
| } catch (Throwable $e) { | ||
| $session->abortTransaction(); | ||
|
|
||
| throw $e; | ||
| } finally { | ||
| $session->endSession(); | ||
| } | ||
| ``` | ||
| `abortTransaction` rolls everything back. `endSession` releases the session and rolls back a transaction that is still open; the underlying connection stays open and can start a new session. | ||
|
|
||
| ## The callback style | ||
|
|
||
| `withTransaction` takes care of commit and rollback for you. It commits when the callback returns and rolls back when it throws, then re-throws the exception: | ||
|
|
||
| ```php | ||
| $session = $client->startSession(); | ||
|
|
||
| $session->withTransaction(static function ($session) use ($accounts): void { | ||
| $accounts->updateOne(['_id' => 'alice'], ['$inc' => ['balance' => -100]], ['session' => $session]); | ||
| $accounts->updateOne(['_id' => 'bob'], ['$inc' => ['balance' => 100]], ['session' => $session]); | ||
| }); | ||
| ``` | ||
| If PostgreSQL rejects the transaction with a serialization failure or a deadlock, `withTransaction` retries the callback a few times before giving up. Make sure the callback has no side effects outside the database, since it may run more than once. | ||
|
|
||
| ## The session option | ||
|
|
||
| Every collection method accepts a `session` option, just like the MongoDB driver. Passing it keeps your code portable. Rango runs all operations on the one connection the client holds, so once a transaction is open on that connection, every following operation takes part in it whether or not you pass the option. | ||
|
|
||
| :::note | ||
| Only pass a session that came from the same client. A session from another client points at a different connection and its transaction would not cover the operation. | ||
| ::: | ||
|
|
||
| ## Isolation level | ||
|
|
||
| By default a transaction runs at PostgreSQL's `READ COMMITTED` level. Pass a `readConcern` to raise it: | ||
|
|
||
| ```php | ||
| $session->startTransaction(['readConcern' => 'snapshot']); | ||
| ``` | ||
| `snapshot` maps to `REPEATABLE READ` and `linearizable` maps to `SERIALIZABLE`. A `SERIALIZABLE` transaction is the case that can fail with a serialization error under concurrency, which is why `withTransaction` retries. | ||
|
|
||
| ## Bulk writes | ||
|
|
||
| `bulkWrite` already runs in its own transaction. Inside a session transaction it joins the surrounding one instead of opening a nested transaction, so a failing bulk write aborts the whole transaction. | ||
|
|
||
| ## Limitations | ||
|
|
||
| * A session runs one transaction at a time. Calling `startTransaction` again before committing or aborting throws. | ||
| * Read the results of `find` and `aggregate` inside the transaction. A cursor that is still open when you commit may not see a consistent snapshot afterwards. | ||
| * Sessions are not causally consistent across connections the way MongoDB sessions are; there is only ever the single client connection. | ||
|
|
||
| ## Learn more | ||
|
|
||
| * [How a client maps to a PostgreSQL connection](connection.md) | ||
| * [How CRUD operations translate to SQL](how-it-works.md) | ||
| * [Bulk writes](crud-operations.md) |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Patchlevel\Rango\Exception; | ||
|
|
||
| use RuntimeException; | ||
|
|
||
| // phpcs:disable SlevomatCodingStandard.Classes.SuperfluousExceptionNaming.SuperfluousSuffix | ||
| final class TransactionException extends RuntimeException implements Exception | ||
| { | ||
| public static function alreadyInProgress(): self | ||
| { | ||
| return new self('A transaction is already in progress for this session'); | ||
| } | ||
|
|
||
| public static function noTransactionStarted(): self | ||
| { | ||
| return new self('There is no transaction started for this session'); | ||
| } | ||
|
|
||
| public static function sessionEnded(): self | ||
| { | ||
| return new self('The session has already been ended'); | ||
| } | ||
|
|
||
| public static function connectionBusy(): self | ||
| { | ||
| return new self('The underlying PDO connection is already running a transaction'); | ||
| } | ||
| } | ||
| // phpcs:enable SlevomatCodingStandard.Classes.SuperfluousExceptionNaming.SuperfluousSuffix |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.