lakshmen
lakshmen

Reputation: 29064

Disconnect all MySQL connections in R

I would like to disconnect from all my SQL database connections in R.

Tried to use closeAllConnections(), but it doesn't disconnect all connections.

Is there a better way to do it?

Upvotes: 0

Views: 2024

Answers (1)

ozanstats
ozanstats

Reputation: 2866

I'd recommend using database connection pooling with the package pool so that you can simply close your pool (poolClose) to get rid of all connections. Pooling will also help you organize your connections and prevent leaking or SQL injections.

References:
https://github.com/rstudio/pool
https://shiny.rstudio.com/articles/pool-basics.html
https://shiny.rstudio.com/articles/pool-advanced.html

Example:

# install the packages if needed
# install.packages("RMySQL")
# install.packages("pool")

library(pool)

pool <- dbPool(
  drv = RMySQL::MySQL(),
  dbname = "dbname",
  host = "host",
  username = "username",
  password = "password"
)

poolClose(pool)

Upvotes: 3

Related Questions