Actually, it looks like even with the pool package I still want an answer to this question :)
Given the following code:
# assumes you have credential info and table name in options()
library(pool)
library(DBI)
library(shiny)
pool <- dbPool(
drv = RMySQL::MySQL(),
user = options()$mysql_db$user,
password = options()$mysql_db$password,
dbname = options()$mysql_db$dbname,
host = options()$mysql_db$host
)
ui <- fluidPage(
numericInput("nrows", "How many rows to show?", 10),
tableOutput("tbl")
)
server <- function(input, output, session) {
output$tbl <- renderTable({
query <- paste0("SELECT * FROM ", options()$mysql_db$table, " LIMIT ",
as.integer(input$nrows)[1], ";")
df <- dbGetQuery(pool, query)
df
})
}
shinyApp(ui, server)
Every time I run the app, a new pool is created, and the previous ones are not deleted. Running
length(DBI::dbListConnections(RMySQL::MySQL()))
reveals that indeed every successive run accumulates another pool object, and after enough times, I get errors because of too many connections.
I assume this won't happen when the app is served via shiny server and is only happening when I'm running it interactively through rstudio, but I'd still like to know the proper place to disconnect.