Postgres connection has been closed error in Spring Boot
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Spring Boot is a widely-used framework to create stand-alone, production-grade applications with minimal configuration. When interacting with databases in a Spring Boot application, one of the common challenges developers face is handling database connections efficiently. This article delves into the issue of "Postgres connection has been closed" error, explaining why it occurs and how to resolve it.
Understanding the Error
A "Postgres connection has been closed" error typically occurs when an attempt is made to execute a query on a connection that has already been terminated. In Spring Boot applications, this problem often surfaces when using connection pooling mechanisms such as HikariCP (the default connection pool in Spring Boot) or a misconfigured DataSource.
Common Causes:
- Idle Connection Timeout: Connections can be closed if they remain idle for too long.
- Network Interruptions: Temporary network issues can lead to connection loss.
- Exhausted Connection Pool: If all connections are in use and not being released back to the pool.
- Misconfigured DataSource Properties: Incorrectly set properties might cause premature closing of connections.
Investigating the Error
Diagnosing a closed connection involves several steps. Here’s how you can go about it:
- Enable Debug Logging:
- Add the following lines to your
application.propertiesorapplication.ymlto capture detailed logs: - Review the logs to identify when the connection was closed and what queries were executed before the error.
- Inspect pool configurations like maximum pool size, connection timeout, idle timeout, and maximum lifetime.
- Modify the
application.propertiesfile to configure HikariCP settings appropriately: - Ensure the
maximum-pool-sizeis set according to your application requirements and server capabilities. - Enable connection testing to ensure idle connections are still valid before usage:
- Use appropriate exception translation mechanisms in Spring to handle database exceptions smoothly.
- Verify network stability and server reliability. Consider deploying in clusters to mitigate network disruptions.
- Monitoring: Utilize monitoring tools like Prometheus or custom JMX metrics to track connection pool usage.
- Database Configuration: Ensure the PostgreSQL server has adequate resources and is configured to handle the expected load.
- Connection Leaks: Detect and fix connection leaks using Spring Boot's configuration
spring.datasource.hikari.leakDetectionThreshold.
Related reading
- Postgres logical replication db table grows indefinitely
- Postgres Replication and Temporary Tables
- Postgres Replication with pglogical ERROR connection to other side has died
- Postgres Sql could not determine data type of parameter by Hibernate
- PostgreSQL row change notify java program and vice versa
- POSTing a OneToMany sub-resource association in Spring Data REST
- PostgreSQL error Fatal role username does not exist
- PostgreSQL restoration throwing error replication slot does not exist

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.