-
Notifications
You must be signed in to change notification settings - Fork 89
docs: Document transaction failure exceptions #572
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
Open
loopassembly
wants to merge
2
commits into
serverpod:main
Choose a base branch
from
loopassembly:docs/transaction-failure-exceptions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,45 @@ var result = await session.db.transaction((transaction) async { | |
|
|
||
| In the example we insert a company and an employee in the same transaction. If any of the operations fail, the entire transaction will be rolled back and no changes will be made to the database. If the transaction is successful, the return value will be `true`. | ||
|
|
||
| ## Transaction failure exceptions | ||
|
|
||
| When the transaction callback throws an exception, Serverpod rolls back the | ||
| transaction and rethrows that exception. | ||
|
|
||
| When the database rejects a query inside the transaction, Serverpod throws a | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The lines hard-wrap at 72 characters. Unwrap to match the rest of the file. (e.g. remove the invisible break line character). |
||
| `DatabaseQueryException`. This can happen, for example, when concurrent writes | ||
| conflict with the selected transaction isolation level, or when PostgreSQL | ||
| detects a deadlock. | ||
|
|
||
| The exact database error code depends on why PostgreSQL rejected the query. | ||
| Serverpod exposes PostgreSQL error code constants through `PgErrorCode`, so you | ||
| can compare them with the `code` field on `DatabaseQueryException`. | ||
|
|
||
| ```dart | ||
| try { | ||
| await session.db.transaction( | ||
| (transaction) async { | ||
| await Company.db.updateRow( | ||
| session, | ||
| company, | ||
| transaction: transaction, | ||
| ); | ||
| }, | ||
| settings: TransactionSettings(isolationLevel: IsolationLevel.serializable), | ||
| ); | ||
| } on DatabaseQueryException catch (e) { | ||
| if (e.code == PgErrorCode.serializationFailure || | ||
| e.code == PgErrorCode.deadlockDetected) { | ||
| // Retry the transaction or report a write conflict to the caller. | ||
| return; | ||
| } | ||
|
|
||
| rethrow; | ||
| } | ||
| ``` | ||
|
|
||
| For all PostgreSQL error codes, see the [PostgreSQL error code appendix](https://www.postgresql.org/docs/current/errcodes-appendix.html). | ||
|
|
||
| ## Transaction isolation | ||
|
|
||
| The transaction isolation level can be configured when initiating a transaction. The isolation level determines how the transaction interacts with concurrent database operations. If no isolation level is supplied, the level is determined by the database engine. | ||
|
|
@@ -47,9 +86,9 @@ In the example the isolation level is set to `IsolationLevel.serializable`. | |
|
|
||
| The available isolation levels are: | ||
|
|
||
| | Isolation Level | Constant | Description | | ||
| |-----------------|-----------------------|-------------| | ||
| | Read uncommitted | `IsolationLevel.readUncommitted` | Exhibits the same behavior as `IsolationLevel.readCommitted` in PostgresSQL | | ||
| | Isolation Level | Constant | Description | | ||
| | --- | --- | --- | | ||
| | Read uncommitted | `IsolationLevel.readUncommitted` | Exhibits the same behavior as `IsolationLevel.readCommitted` in PostgreSQL | | ||
|
Swiftaxe marked this conversation as resolved.
|
||
| | Read committed | `IsolationLevel.readCommitted` | Each statement in the transaction sees a snapshot of the database as of the beginning of that statement. | | ||
| | Repeatable read | `IsolationLevel.repeatableRead` | The transaction only observes rows committed before the first statement in the transaction was executed giving a consistent view of the database. If any conflicting writes among concurrent transactions occur, an exception is thrown. | | ||
| | Serializable | `IsolationLevel.serializable` | Gives the same guarantees as `IsolationLevel.repeatableRead` but also throws if read rows are updated by other transactions. | | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is a repetition of line 7, and can be omitted.