0

I'm trying to automate data download from db using RJDBC using a for loop. The database im using automatically closes the connection after every 10mins, so what i want to do is somehow catch the error, remake the connection, and continue the loop. In order to do this i need to capture the error somehow, the problem is, it is not an r error so none of the commands trycatch and similar works. I just get a text on the console telling me:

Error in .jcheck() : No running JVM detected. Maybe .jinit() would help.

How do i handle this in terms of:

if (output == ERROR) {remake connection and run dbQuery} else {run dbQuery}

thanks for any help

adl
  • 1,390
  • 16
  • 36

1 Answers1

1

You could use the pool package to abstract away the logic of connection management.
This does exactly what you expect regarding connection management with DBI.
It should work with RJDBC which is an implentation of DBI, but I didn't test it with this driver.

libray(pool)
library(RJDBC)

conn <- dbPool(
  drv = RJDBC::JDBC(...),
  dbname = "mydb",
  host = "hostadress",
  username = "test",
  password = "test"
)
on.exit(poolClose(conn))

dbGetQuery(conn, "select... ")

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • the problem is, the connection closes automatically. What i want to do is to check if it is closes while my loop is ongoing – adl Jun 06 '21 at 08:28
  • If the connection closes, `pool` will reopen it as soon as you query again the database. Does the connection close while a query in running? – Waldi Jun 06 '21 at 10:06
  • it usually closes while the for loop is running, so i need to remake it if its closed – adl Jun 06 '21 at 11:43
  • To help further, I would need some more detail on the way the loop is working : could you provide a code example? `pool` will reopen automatically a closed connection. Did you try? – Waldi Jun 06 '21 at 12:11