Skip to content

Commit

Permalink
Remove unpreparedTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-volkov committed Jan 6, 2025
1 parent b183d4f commit 15058e2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.2

- Removed the `unpreparedTransaction` session because the same effects can now be achieved via the connection settings in Hasql

# 1.1

- Add automatic retry on deadlock errors (code 40P01)
24 changes: 12 additions & 12 deletions library/Hasql/Transaction/Private/Sessions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ We may want to
do one transaction retry in case of the 23505 error, and fail if an identical
error is seen.
-}
inRetryingTransaction :: IsolationLevel -> Mode -> Session (a, Bool) -> Bool -> Session a
inRetryingTransaction level mode session preparable =
inRetryingTransaction :: IsolationLevel -> Mode -> Session (a, Bool) -> Session a
inRetryingTransaction level mode session =
fix $ \retry -> do
attemptRes <- tryTransaction level mode session preparable
attemptRes <- tryTransaction level mode session
case attemptRes of
Just a -> return a
Nothing -> retry

tryTransaction :: IsolationLevel -> Mode -> Session (a, Bool) -> Bool -> Session (Maybe a)
tryTransaction level mode body preparable = do
statement () (Statements.beginTransaction level mode preparable)
tryTransaction :: IsolationLevel -> Mode -> Session (a, Bool) -> Session (Maybe a)
tryTransaction level mode body = do
statement () (Statements.beginTransaction level mode)

bodyRes <- catchError (fmap Just body) $ \error -> do
statement () (Statements.abortTransaction preparable)
statement () Statements.abortTransaction
handleTransactionError error $ return Nothing

case bodyRes of
Just (res, commit) -> catchError (commitOrAbort commit preparable $> Just res) $ \error -> do
Just (res, commit) -> catchError (commitOrAbort commit $> Just res) $ \error -> do
handleTransactionError error $ return Nothing
Nothing -> return Nothing

commitOrAbort :: Bool -> Bool -> Session ()
commitOrAbort commit preparable =
commitOrAbort :: Bool -> Session ()
commitOrAbort commit =
if commit
then statement () (Statements.commitTransaction preparable)
else statement () (Statements.abortTransaction preparable)
then statement () Statements.commitTransaction
else statement () Statements.abortTransaction

handleTransactionError :: SessionError -> Session a -> Session a
handleTransactionError error onTransactionError = case error of
Expand Down
18 changes: 9 additions & 9 deletions library/Hasql/Transaction/Private/Statements.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import Hasql.Transaction.Private.SQL qualified as D

-- * Transactions

beginTransaction :: IsolationLevel -> Mode -> Bool -> A.Statement () ()
beginTransaction isolation mode preparable =
A.Statement (D.beginTransaction isolation mode) B.noParams C.noResult preparable
beginTransaction :: IsolationLevel -> Mode -> A.Statement () ()
beginTransaction isolation mode =
A.Statement (D.beginTransaction isolation mode) B.noParams C.noResult True

commitTransaction :: Bool -> A.Statement () ()
commitTransaction preparable =
A.Statement "COMMIT" B.noParams C.noResult preparable
commitTransaction :: A.Statement () ()
commitTransaction =
A.Statement "COMMIT" B.noParams C.noResult True

abortTransaction :: Bool -> A.Statement () ()
abortTransaction preparable =
A.Statement "ABORT" B.noParams C.noResult preparable
abortTransaction :: A.Statement () ()
abortTransaction =
A.Statement "ABORT" B.noParams C.noResult True

-- * Streaming

Expand Down
6 changes: 3 additions & 3 deletions library/Hasql/Transaction/Private/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ instance (Monoid a) => Monoid (Transaction a) where
-- |
-- Execute the transaction using the provided isolation level and mode.
{-# INLINE run #-}
run :: Transaction a -> IsolationLevel -> Mode -> Bool -> B.Session a
run (Transaction session) isolation mode preparable =
D.inRetryingTransaction isolation mode (runStateT session True) preparable
run :: Transaction a -> IsolationLevel -> Mode -> B.Session a
run (Transaction session) isolation mode =
D.inRetryingTransaction isolation mode (runStateT session True)

-- |
-- Possibly a multi-statement query,
Expand Down
14 changes: 1 addition & 13 deletions library/Hasql/Transaction/Sessions.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module Hasql.Transaction.Sessions
( transaction,
unpreparedTransaction,

-- * Transaction settings
C.Mode (..),
C.IsolationLevel (..),
)
where

import Data.Bool
import Hasql.Session qualified as B
import Hasql.Transaction.Config qualified as C
import Hasql.Transaction.Private.Transaction qualified as A
Expand All @@ -18,14 +16,4 @@ import Hasql.Transaction.Private.Transaction qualified as A
{-# INLINE transaction #-}
transaction :: C.IsolationLevel -> C.Mode -> A.Transaction a -> B.Session a
transaction isolation mode transaction =
A.run transaction isolation mode True

-- |
-- Execute the transaction using the provided isolation level and mode,
-- and specifying that the generated BEGIN, COMMIT and ABORT statements should not be prepared.
--
-- Helps with transaction pooling due to its incompatibility with prepared statements.
{-# INLINE unpreparedTransaction #-}
unpreparedTransaction :: C.IsolationLevel -> C.Mode -> A.Transaction a -> B.Session a
unpreparedTransaction isolation mode transaction =
A.run transaction isolation mode False
A.run transaction isolation mode

0 comments on commit 15058e2

Please sign in to comment.