If you are using a single app.R file you can close the connection on stop, by putting this before calling shinyApp(ui = ui, server = server)
onStop(function() {
dbDisconnect(con)
})
# Run the application
shinyApp(ui = ui, server = server)
EDIT: You might also have to pass session variable to your server function
server <- function(input, output, session) {