I would like to understand what is the best practice for (re)using SQL connections to a MS SQL database through RJDBC.
I can imagine three possible scenarios:
- Store a connection in a global variable, initialize once, use it everywhere in the code
- Create a new connection for every request
- Do something more complicated, e.g. pre-populate a pool of open connection, and (re)use connections from the pool as needed.
I'm using my code in a shiny application with several dozens clients, and I'm afraid that something bad will happen if I use method 1. So I use method 2, creating a new connection for every request with the code below.
I can see some potential downsides of this approach: performance, taxing database resources, etc. But may be I'm too cautious since R is single-threaded, even in shiny usage scenario?
So my specific questions are:
A. Can I safely use a single connection to MS SQL database through RJDBC throughout my shiny application?
B. Are there any real downsides (memory leakage, performance, etc.) in scenario 2 above?
NewConnection <- function() {
file = NULL
# make it work on three different OSes - Linux, MacOS, Windows
for (path in c('/Users/victor/Documents/R/sqljdbc_3.0/enu/sqljdbc4.jar',
'/home/oracle/sqljdbc_3.0/enu/sqljdbc4.jar',
'C:/Projects/jdbc/sqljdbc_4.0/enu/sqljdbc4.jar')) {
if (file.exists(path)) {
file = path
break
}
}
if (is.null(file))
return(NULL)
else {
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver", file)
passwd <- GetUserNamePassword()
conn <- dbConnect(drv, "jdbc:sqlserver://sql.server.address.com",
passwd$username, passwd$password)
return(conn)
}
}
P.S. Related: How to manage a database connection in an R Package