mysqlhaskellodbchdbc

haskell `hdbc` ODBC connection gets disposed of immediately for remote MySQL instance


I am trying to connect to a MySQL database and run an SQL query using hdbc and hdbc-odbc

main :: IO ()
main = do
  mysqlSettings <- readMySQLSettings
  putStr "Connecting to MySQL database..."
  mysqlConn <- connectODBC $ buildMySQLConnectionString mysqlSettings
  putStrLn "Connected"
  _ <- run mysqlConn "USE np" []
  putStrLn " Done."

The database connects fine but subsequently when it runs an SQL query (_ <- run mysqlConn "USE np" []) I get the following error.

SqlError {seState = "", seNativeError = -1, seErrorMsg = "Tried to use a disposed ODBC Connection handle"}

To my understanding it seems like the error says that the connection gets immediately freed as soon as it's created. This problem only happens when connecting to a remote database (Amazon RDS in this case) and does not for my local MySQL instance.


Solution

  • Turns out the problem is in the cpp-options I added blindly to the cabal file.

        ...
       
        cpp-options: -DDCABAL_BUILD_DEVELOPER
    
        build-depends:
            base >=4.14.2.0
            ...
    

    I have no idea what DDCABAL_BUILD_DEVELOPER is used for. Disabling the cpp-options fixed the issue.