简体   繁体   English

haskell `hdbc` ODBC 连接立即被远程 MySQL 实例处理掉

[英]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我正在尝试连接到 MySQL 数据库并使用hdbchdbc-odbc运行 SQL 查询

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.数据库连接正常,但随后当它运行 SQL 查询( _ <- run mysqlConn "USE np" [] )时,我收到以下错误。

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.此问题仅在连接到远程数据库(在本例中为 Amazon RDS)时发生,而不适用于我的本地 MySQL 实例。

I guess this happens when you're using an ODBC driver which internally uses libmysqlclient .我猜当您使用内部使用libmysqlclient的 ODBC 驱动程序时会发生这种情况。 The thing is, libmysqlclient doesn't restart system calls interrupted by signals, and GHC runtime uses signals internally.问题是, libmysqlclient不会重新启动被信号中断的系统调用,而 GHC 运行时在内部使用信号。

You can see how you can avoid this by looking at Database.HDBC.MySQL.withRTSSignalsBlocked .您可以通过查看Database.HDBC.MySQL.withRTSSignalsBlocked来了解如何避免这种情况。 Simply put, this function blocks SIGALRM and SIGVTALRM which are used by GHC runtime while executing your code block.简而言之,此函数会阻止 GHC 运行时在执行代码块时使用的SIGALRMSIGVTALRM You may directly use this function, or you can just copy it if you don't want to depend on HDBC-mysql .这个函数可以直接使用,如果不想依赖HDBC-mysql也可以直接复制。

Turns out the problem is in the cpp-options I added blindly to the cabal file.原来问题出在我盲目添加到cabal文件中的cpp-options

    ...
   
    cpp-options: -DDCABAL_BUILD_DEVELOPER

    build-depends:
        base >=4.14.2.0
        ...

I have no idea what DDCABAL_BUILD_DEVELOPER is used for.我不知道DDCABAL_BUILD_DEVELOPER是做什么用的。 Disabling the cpp-options fixed the issue.禁用cpp-options解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM