Skip to content

PR 42: withTransactionModeRetry #54

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 4 commits into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
* Add `Identity` and `Const` instance
Thanks to Cary Robbins for the implementation
https://github.com/haskellari/postgresql-simple/pull/46
* Add `withTransactionModeRetry'`, a variant of `withTransactionModeRetry`
for all exception types.
Thanks to Elliot Cameron for the implementation
https://github.com/haskellari/postgresql-simple/pull/42
* Fix spurious aborts when retrying transactions
Thanks to Elliot Cameron for the implementation
https://github.com/haskellari/postgresql-simple/pull/34

### Version 0.6.2 (2019-04-26)

Expand Down
28 changes: 16 additions & 12 deletions src/Database/PostgreSQL/Simple/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Database.PostgreSQL.Simple.Transaction
, withTransactionLevel
, withTransactionMode
, withTransactionModeRetry
, withTransactionModeRetry'
, withTransactionSerializable
, TransactionMode(..)
, IsolationLevel(..)
Expand Down Expand Up @@ -146,31 +147,34 @@ withTransactionMode mode conn act =
commit conn
return r

-- | 'withTransactionModeRetry'' but with the exception type pinned to 'SqlError'.
withTransactionModeRetry :: TransactionMode -> (SqlError -> Bool) -> Connection -> IO a -> IO a
withTransactionModeRetry = withTransactionModeRetry'

-- | Like 'withTransactionMode', but also takes a custom callback to
-- determine if a transaction should be retried if an 'SqlError' occurs.
-- If the callback returns True, then the transaction will be retried.
-- If the callback returns False, or an exception other than an 'SqlError'
-- determine if a transaction should be retried if an exception occurs.
-- If the callback returns 'True', then the transaction will be retried.
-- If the callback returns 'False', or an exception other than an @e@
-- occurs then the transaction will be rolled back and the exception rethrown.
--
-- This is used to implement 'withTransactionSerializable'.
withTransactionModeRetry :: TransactionMode -> (SqlError -> Bool) -> Connection -> IO a -> IO a
withTransactionModeRetry mode shouldRetry conn act =
withTransactionModeRetry' :: forall a e. E.Exception e => TransactionMode -> (e -> Bool) -> Connection -> IO a -> IO a
withTransactionModeRetry' mode shouldRetry conn act =
mask $ \restore ->
retryLoop $ E.try $ do
a <- restore act
a <- restore act `E.onException` rollback_ conn
commit conn
return a
where
retryLoop :: IO (Either E.SomeException a) -> IO a
retryLoop :: IO (Either e a) -> IO a
retryLoop act' = do
beginMode mode conn
r <- act'
case r of
Left e -> do
rollback_ conn
case fmap shouldRetry (E.fromException e) of
Just True -> retryLoop act'
_ -> E.throwIO e
Left e ->
case shouldRetry e of
True -> retryLoop act'
False -> E.throwIO e
Right a ->
return a

Expand Down