Safely closes a database connection. Checks if the connection is valid before attempting to disconnect.
Arguments
- con
A connection object as produced by
db_connectorDBI::dbConnect.
Details
This function wraps RPostgres::dbDisconnect() with a validity check
to avoid errors when disconnecting an already-closed connection.
Examples
if (FALSE) { # \dontrun{
# Not run:
# Connect and disconnect
con <- db_connect()
# ... perform database operations ...
db_disconnect(con)
# Safe to call on.exit to ensure cleanup
con <- db_connect()
on.exit(db_disconnect(con), add = TRUE)
} # }