dbPool {pool} | R Documentation |
Create a pool of database connections
Description
dbPool()
is a drop-in replacement for DBI::dbConnect()
that
provides a shared pool of connections that can automatically reconnect
to the database if needed.
Usage
dbPool(
drv,
...,
minSize = 1,
maxSize = Inf,
onCreate = NULL,
idleTimeout = 60,
validationInterval = 60,
validateQuery = NULL
)
Arguments
drv |
A DBI Driver, e.g. |
... |
Arguments passed on to |
minSize , maxSize |
The minimum and maximum number of objects in the pool. |
onCreate |
A function that takes a single argument, a connection,
and is called when the connection is created. Use this with
|
idleTimeout |
Number of seconds to wait before destroying idle objects
(i.e. objects available for checkout over and above |
validationInterval |
Number of seconds to wait between validating objects that are available for checkout. These objects are validated in the background to keep them alive. To force objects to be validated on every checkout, set
|
validateQuery |
A simple query that can be used to verify that the
connetction is valid. If not provided, |
Examples
# You use a dbPool in the same way as a standard DBI connection
pool <- dbPool(RSQLite::SQLite())
pool
DBI::dbWriteTable(pool, "mtcars", mtcars)
dbGetQuery(pool, "SELECT * FROM mtcars LIMIT 4")
# Always close a pool when you're done using it
poolClose(pool)
# Using the RMySQL package
if (requireNamespace("RMySQL", quietly = TRUE)) {
pool <- dbPool(
drv = RMySQL::MySQL(),
dbname = "shinydemo",
host = "shiny-demo.csa7qlmguqrf.us-east-1.rds.amazonaws.com",
username = "guest",
password = "guest"
)
dbGetQuery(pool, "SELECT * from City LIMIT 5;")
poolClose(pool)
}