Skip to content

Commit

Permalink
Add the resetting of the registry in case of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-volkov committed Sep 13, 2024
1 parent 8c3685e commit 6bb52d0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 11 additions & 1 deletion library/Hasql/PreparedStatementRegistry.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ module Hasql.PreparedStatementRegistry
( PreparedStatementRegistry,
new,
update,
reset,
LocalKey (..),
)
where

import ByteString.StrictBuilder qualified as B
import Data.HashTable.IO qualified as A
import Hasql.LibPq14 qualified as Pq
import Hasql.Prelude hiding (lookup)
import Hasql.Prelude hiding (lookup, reset)

data PreparedStatementRegistry
= PreparedStatementRegistry !(A.BasicHashTable LocalKey ByteString) !(IORef Word)
Expand Down Expand Up @@ -42,6 +43,15 @@ update localKey onNewRemoteKey onOldRemoteKey (PreparedStatementRegistry table c
old =
onOldRemoteKey

reset :: PreparedStatementRegistry -> IO ()
reset (PreparedStatementRegistry table counter) = do
-- TODO: This is a temporary measure.
-- We should just move to a pure implementation.
do
entries <- A.toList table
forM_ entries \(k, _) -> A.delete table k
writeIORef counter 0

-- |
-- Local statement key.
data LocalKey
Expand Down
7 changes: 5 additions & 2 deletions library/Hasql/Session/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Hasql.IO qualified as IO
import Hasql.LibPq14 qualified as Pq
import Hasql.Pipeline.Core qualified as Pipeline
import Hasql.Prelude
import Hasql.PreparedStatementRegistry qualified as PreparedStatementRegistry
import Hasql.Statement qualified as Statement

-- |
Expand All @@ -29,11 +30,13 @@ run (Session impl) connection =
runExceptT $ runReaderT impl connection
handler =
case connection of
Connection.Connection pqConnVar _ _ ->
Connection.Connection pqConnVar _ registry ->
withMVar pqConnVar \pqConn ->
Pq.transactionStatus pqConn >>= \case
Pq.TransIdle -> pure ()
_ -> Pq.reset pqConn
_ -> do
PreparedStatementRegistry.reset registry
Pq.reset pqConn

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

0 comments on commit 6bb52d0

Please sign in to comment.