closing database connection when closing shiny app

If I close a database pool when ending a session all remainig user with an open app will lose their database connection.

session$onSessionEnded(function() {


Is there a way to avoid this habbit?
Will the pool close its open connection when gc runs?

Hey there!

I think you might wanna check out session$onStop. It allows you to register a callback that will be executed when the app exits.

FYI, there is a typo on the code snippet (it says "poolColse": pool::poolColse(con_postgres)) :sweat_smile:

This post was published by an Appsilon team member. Our company can help you get the most out of RShiny and Posit/RStudio products.

Check our open positions here.

Appsilon: Building impactful RShiny Dashboards and providing R coding services.

Hello tested. works as expected I put the callback Function into my global.R file

con_sql_server <-   pool::dbPool(drv = odbc::odbc(), dsn = "my_dsn",
                      Uid = "my_uid",
                      Pwd = "my_pwd")

con_postgres <-   pool::dbPool(drv = odbc::odbc(), dsn =  "PostgreSQL")

onStop(function() {

  cat("Closing Database Connections")



Thank's a lot!

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.