-
-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sa: wrap transactions in a function for commits/rollbacks (#4373)
In the current SA code, we need to remember to call Rollback on any error. If we don't, we'll leave dangling transactions, which are hard to spot but eventually clog up the database and cause availability problems. This change attempts to deal with rollbacks more rigorously, by implementing a withTransaction function that takes a closure as input. withTransaction opens a transaction, applies a context.Context to it, and then runs the closure. If the closure returns an error, withTransaction rolls back and return the error; otherwise it commits and returns nil. One of the quirks of this implementation is that it relies on the closure modifying variables from its parent scope in order to return values. An alternate implementation could define the return value of the closure as interface{}, nil, and have the calling function do a type assertion. I'm seeking feedback on that; not sure yet which is cleaner. This is a subset of the functions that need this treatment. I've got more coming, but some of the changes break tests so I'm checking into why. Updates #4337
- Loading branch information
1 parent
eb20b2a
commit 16235b6
Showing
2 changed files
with
196 additions
and
167 deletions.
There are no files selected for viewing
This file contains 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.