R:postgres 连接不断超时或中断

R: postgres connection keeps timing out or breaking

我正在 运行使用 R 编写一个代码,它连接到 postgresql 数据库。连接是在循环外定义的,但它会超时并不断中断。如果我将连接放在循环中,并在每次使用它时终止它。我们达到连接数限制。

此外,当我们 运行 循环中的 r 代码时,answers/outputs 将其存储在数据库中,它在前 15 分钟内有效,但随后连接中断,表示无法连接。

我收到以下错误:

    RS-DBI driver: (could not connect ------ on dbname "abc": could not connect to server: Connection timed out (0x0000274C/10060)
    Is the server running on host "123.456.567.890" and accepting
    TCP/IP connections on port 5432?
)Error in diagnosticTestsPg(project_path, modelbank, modelproduct, modelwaterfall,  : 
  object 'conn' not found
In addition: There were 50 or more warnings (use warnings() to see the first 50)

这里conn是数据库的连接

有没有办法解决这个问题或解决方法以在循环 运行s 之前建立连接?

id <- tryCatch(withCallingHandlers(
    id <- f(), error=function(e) {
      write.to.log(sys.calls())
      }, 
    warning=function(w) {
      write.to.log(sys.calls())
      invokeRestart("muffleWarning")
      }
    )
  , error = function(e) { print("recovered from error") }
  )

其中 f() 具有数据库连接详细信息