Every time I source my R script it leaks a db connection

155 views Asked by At

I cannot paste the entire script here, but I am explaining the situation. If you have ever got leaked DB connections then you would be knowing what I am talking about.

I have an R script file that has many functions (around 50) that use db connections using the DBI & RMySQL R packages. I have consolidated all DB access through 4 or 5 functions. I use on.exit(dbDisconnect(db)) in every single function where a dbConnect is used.

I discovered that just on loading this script using source("dbscripts.R") causes one DB connection to leak. I see this when I run the command

dbListConnections(MySQL())

[[1]] MySQLConnection:0,607>

[[2]] MySQLConnection:0,608>

[[3]] MySQLConnection:0,609>

[[4]] MySQLConnection:0,610>

I see one more DB connection added to the list everytime. This quickly reaches to 16 and my script stops working.

The problem is, I am unable to find out which line of code is causing the leak. I have checked each dbConnect line in the code. All of them are within functions and no dbConnect happens outside in the main code.

So, why is the connection leak occurring?

0

There are 0 answers