RStudio Shiny Error mysqlNewConnection maximum of 16 connections

719016

I got a shiny server running that connects to a MySQL database. The page that was so far working fine is giving me this error now:

Error in mysqlNewConnection(drv, ...) : RS-DBI driver: 
(cannot allocate a new connection -- maximum of 16 connections already opened)

Which makes me wonder how should I be handling open mysql connections in an interactive webpage.

Firstly, should the dbConnect(MySQL(),...) statement be before the shinyServer method or inside?

If I add a dbDisconnect(dbcon) at the end of server.R, then I get an Error: expired MysqLConnection error from the page, and does not show any data.

I tried with this as well inside or before the shinyServer method:

 on.exit(dbDisconnect(dbcon), add=TRUE)

or

 on.exit(dbDisconnect(dbcon))

So the code for the page itself only works for me if I leave the connection open, which I suppose then can cause the maximum connections error above.

How to handle these situations in Shiny?

jdharrison

You can set the connection in the global.R file or outside shinyServer an example from https://groups.google.com/forum/#!topic/shiny-discuss/0VjQc2a6z3M is:

library(RMySQL)

getConnection <- function(group) {

  if (!exists('.connection', where=.GlobalEnv)) {
    .connection <<- dbConnect(MySQL(), group=group)
  } else if (class(try(dbGetQuery(.connection, "SELECT 1"))) == "try-error") {
    dbDisconnect(.connection)
    .connection <<- dbConnect(MySQL(), group=group)
  }

  return(.connection)
}

This defines a functions that checks for a connection in the global env. If one is not found it creates one. If one is found but cannot be connected to then the connection is restarted. No explicit disconnect is given so I guess the connection is just allowed to timeout eventually.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

RStudio Shiny ERROR: there is no package called "shinydashboard"

From Dev

RStudio Server and Shiny Server Websocket Error 400: The fix for Shiny Server didn't work for RStudio Server

From Dev

RStudio - Shiny - Error "Operation not allowed without an active reactive context"

From Dev

RStudio Shiny Error - number of items to replace is not a multiple of replacement length

From Dev

Shiny app run well in Rstudio but file not found error shiny-server

From Dev

shiny causes RStudio to crash

From Dev

Rstudio shiny conditionalPanel for mainPanel

From Dev

Rstudio shiny collapsible sidePanel

From Dev

RStudio Shiny dynamic selectize

From Dev

RStudio and Shiny in one dockerfile

From Dev

Shiny (Rstudio) apps not working

From Dev

Shiny (Rstudio) apps not working

From Dev

RStudio Shiny dynamic selectize

From Dev

What is the maximum number of connections?

From Dev

RStudio Shiny renderDataTable font size

From Dev

Rstudio shiny not able to use ggvis

From Dev

zoomable image map in RStudio Shiny

From Dev

Publish Rstudio Shiny App in intranet

From Dev

Rstudio and shiny server proxy setting

From Dev

Scale and size of plot in RStudio shiny

From Dev

using RStudio as an pseudo shiny app

From Dev

RStudio and Shiny: define a reactiveValues with a Reactive

From Dev

Setting up maximum of connections for web

From Dev

Maximum websocket connections per client

From Dev

Rstudio shiny not finding image in www folder

From Dev

Rstudio Shiny reactive options list in renderDataTable

From Dev

Rstudio shiny ggvis tooltip on mouse hover

From Dev

Rstudio shiny renderDataTable headers multi line?

From Dev

RStudio Shiny list from checking rows in dataTables

Related Related

HotTag

Archive