title: RPostgreSQL Cannot Close Connections
body: <p>I have a shiny app that connects to a database using <code>RPostgreSQL</code>. At the end of the app the connection is closed and the driver should be unloaded but I get an error, warning me that the connection is not closed.</p>
  
  <p>The code looks something like this:</p>
  
  <pre><code> # in the app.R file, but not in the server function:
   drv &lt;- dbDriver("PostgreSQL")
   con &lt;- dbConnect(drv, dbname = "database1",
                  host = "localhost", port = 5432,
                  user = "user", password = "pw")
  
  # in the server function:
  foo &lt;- dbGetQuery(con, "SELECT * from table1")
  
  # at the end of the server function to disconnect when the app is closed:
  session$onSessionEnded(function(){
      dbDisconnect(con)
      dbUnloadDriver(drv)
  })
  </code></pre>
  
  <p>However, I get the error message: <code>Error in postgresqlCloseDriver(drv, ...): RS-DBI driver: (There are opened connections -- close them first)</code> this is displayed with the command <code>dbUnloadDriver(drv)</code>. </p>
  
  <p>When I manually look for open connections with <code>dbListConnections()</code> I get a list with up to 16 open connections to the database. Notice, I only use <code>dbGetQuery</code> never <code>dbSendQuery</code> to avoid having to close connections.</p>
  
  <p>Any ideas?</p>