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

Time:11-16

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.

CodePudding user response:

I guess this happens when you're using an ODBC driver which internally uses libmysqlclient. The thing is, libmysqlclient doesn't restart system calls interrupted by signals, and GHC runtime uses signals internally.

You can see how you can avoid this by looking at Database.HDBC.MySQL.withRTSSignalsBlocked. Simply put, this function blocks SIGALRM and SIGVTALRM which are used by GHC runtime while executing your code block. You may directly use this function, or you can just copy it if you don't want to depend on HDBC-mysql.

CodePudding user response:

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.

  • Related