简体   繁体   中英

R Shiny App Disconnects from Database In R Shiny Pro

I have a complicated R Shiny app where I was using R Shiny pool, with ROracle backend, to connect to an oracle database. I made a simpler version and I was able to still reproduce the error. The database pool/connection, whichever is used, seems to work for a bit, and then randomly disconnects. In the larger app, it works the first time, and all subsequent requests fail. The code works fine through RStudio on my laptop, though that's deployed through Windows instead of Linux and R Shiny Pro. I am not calling disconnect on the POOL at anytime.

ui.R

library(shiny)
library(shinythemes)
library(dplyr)
library(readr)

ui <- fluidPage(theme = shinytheme("lumen"),
    titlePanel("Google Trend Index"),
    sidebarLayout(
        sidebarPanel(
        ),
        mainPanel(
            DT::dataTableOutput("db_studies", width = "100%"),
            DT::dataTableOutput("db_studies2", width = "100%")
        )
    )
)

server.R

library(DT)

POOL <<- MyDB::openPool()
server <- function(input, output, session) {
    output$db_studies <- DT::renderDataTable(
        DT::datatable(
            DBI::dbReadTable(POOL, "STUDY") %>%
                data.table::data.table() %>%
                dplyr::arrange(desc(DATE_CREATED)),
        )
    )

    output$db_studies2 <- DT::renderDataTable(
        DT::datatable(
            dplyr::tbl(POOL, "SAMPLE") %>%
                dplyr::filter(DATASET_ID %in% 57) %>%
                dplyr::collect(n = Inf)
        )
    )
}

The pool code is encapsulated in a package:

openPool <- function(..., keyFilename = "db_key.RData",
    credentialsFilename = "db_login.txt",
    connectionInfoFilename = "db_connection.Rdata") {
    credentials <- getCredentials(keyFilename = keyFilename, filename = credentialsFilename)
    connectionInfo <- getConnectionInfo(filename = connectionInfoFilename)

    POOL <- pool::dbPool(...,
        drv = ROracle::Oracle(),
        dbname = paste(
            "(DESCRIPTION=",
            "(ADDRESS=(PROTOCOL=tcp)",
            "(HOST=", connectionInfo$host, ")",
            "(PORT=", connectionInfo$port, "))",
            "(CONNECT_DATA=(SID=", connectionInfo$sid, ")))", sep = ""),
        host = connectionInfo$host,
        port = connectionInfo$port,
        username = credentials$username,
        password = credentials$password
    )

    POOL
}

The purpose of the package was to separate out the database credentials and handling, but for the simple test above I query the database directly, and still after a few refreshes the database pool becomes invalid and fails.

I initially instantiated the POOL object, and passed it around. Seeing how none of the examples do this, I instead created a global POOL object which I then tried to reference directly. I also tried to use a direct connection, either through openConnection or through the pool's checkout , and both disconnect after a few tries.

There are few things you can do:

## In your pool function, put maximum connection in drv as below 
drv = ROracle::Oracle(max.con=128)

Also, there is always often problem with pool leaking connections. So, in your case, I think it is reaching maximum connection which is 16 by default. To avoid this, I would suggest using below line after your data has been loaded from oracle database. This will delete all your leaked connection.

lapply(dbListConnections(Oracle()), dbDisconnect)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM