We have a Postgres database set up in the RDS. I wrote some APIs to handle data ingestion to the db using Spring Boot. Recently I discovered that lots of connections kept their sessions alive after calling the APIs. Some of the sessions date back to 3 months ago.
I wonder if there is a way to automatically close these connections after it's been inactive for a while. From How to close idle connections in PostgreSQL automatically?, looks like I can set up a cron job to look for dead connections with a SQL query and terminate them with pg_trminate_backend
. Is this the best option? Is there something that can be done in the web application layer? Or maybe some RDS parameters? Need some advice on this.