How to connect a shiny application with a database without generating errors on reconnections?

I have an app on the server that retrieves all needed data from the database at the beginning of its work.

It works once, twice, but if too many people open the app simultaneously or refresh too fast, too many connections are being made and it "freezes" and never actually load the data properly anymore.

In logs I have found a .log file that once stated: "Error: C stack usage is too close to limit" but there is no possibility of recursion of anything as the app looks something like this:

pacman::p_load(shiny, RPostgres, pool, future, promises)

pool <- dbPool(Postgres(),
               dbname = ...)

database_table <- dbGetQuery(pool, "select * from table")

ui <- fluidPage(tableOutput('tab'))

server <- function(input, output, session) {
  output$tab <- renderTable({

shinyApp(ui, server)

Changing the code to asynchronous does not fix anything. It still freezes and I have to wait some time to make it work after a refresh.

database_table <- future({
  pool <- dbPool(Postgres(),
                 dbname = ...)
  dbGetQuery(pool, "select * from table")

Closing a pool object does not fix it neither.

onStop(function() {

Then, what is the best way to have a connection to the database in the Shiny app?

Is it even a good practice to load everything at the beginning or each time after someone actually clicks something that demands database data?

I discovered that this is not a problem with the database connection but with Shiny Server itself.

When opening multiple sessions in a short period of time, the server.R with only the server function(input, output, session) does not seem to be working at all. Is this a Shiny Server limitation?

Everything else that is run on the server (global.R and ui.R) works great, but the session part which is supposed to be loaded for different users is not.

I have gotten the mysterious Stack Usage Error and the equally mysterious solution was to get rid of the RJava package. Are you loading RJava? - Tom

No I am not. It breaks even with pure Shiny and nothing else.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.