Skip to content

Commit

Permalink
Add connection reset on exceptions in non-idle transaction status
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-volkov committed Sep 13, 2024
1 parent f033b12 commit 52cfda4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions hasql.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ common base
StandaloneDeriving
StrictData
TupleSections
TypeApplications
TypeFamilies
TypeOperators

Expand Down
15 changes: 14 additions & 1 deletion library/Hasql/Session/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Hasql.Encoders.All qualified as Encoders
import Hasql.Encoders.Params qualified as Encoders.Params
import Hasql.Errors
import Hasql.IO qualified as IO
import Hasql.LibPq14 qualified as Pq
import Hasql.Pipeline.Core qualified as Pipeline
import Hasql.Prelude
import Hasql.Statement qualified as Statement
Expand All @@ -22,8 +23,20 @@ newtype Session a
-- Executes a bunch of commands on the provided connection.
run :: Session a -> Connection.Connection -> IO (Either SessionError a)
run (Session impl) connection =
runExceptT
handle @SomeException onExc
$ runExceptT
$ runReaderT impl connection
where
onExc exc =
case connection of
Connection.Connection pqConnVar _ _ ->
withMVar pqConnVar onPqConn
where
onPqConn pqConn = do
Pq.transactionStatus pqConn >>= \case
Pq.TransIdle -> pure ()
_ -> Pq.reset pqConn
throwIO exc

-- |
-- Possibly a multi-statement query,
Expand Down

0 comments on commit 52cfda4

Please sign in to comment.