R: postgres connection keeps timing out or breaking

0

I am running a code in R, which connects to the postgresql database. The connection is defined outside the loop, but it times out and keeps breaking. If I put the connection inside the loop, and kill it each time I use it. We reach the limit on the connections.

Additionally, when we run the r code in a loop, the answers/outputs are stored it in a db, it works for first 15 minutes but then the connection breaks saying it cannot connect.

I get the following errors:

    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)

Here, conn is the connection to the database

enter image description here Is there a way to fix this or a workaround to have the connection in place until the loop runs?

r
postgresql
connection
asked on Stack Overflow Apr 5, 2019 by Bruce Wayne • edited Apr 15, 2019 by Bruce Wayne

1 Answer

0
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") }
  )

Where f() has the db connection details

answered on Stack Overflow Apr 16, 2019 by Bruce Wayne

User contributions licensed under CC BY-SA 3.0