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?