Rstudio published a series of article back in june about best practices to connect to a database. The simple answer is to use pools (see here and here). To make it simple, you define your pool once, and it will handle and manage the connections, opening and closing them as needed. Once the app is disconnected, the pool will close all the connections automatically.
Unfortunately, the pool package doesn't work with SQL Server and ODBC. For that situation (or if you don't want to use pool), they advice to use on.exit inside your server functions.
for example:
getData <- reactive({
cnxn <- dbConnect(...)
on.exit(dbDisconnect(cnxn))
... # your stuff here
})