Spring Boot
Postgres
database connection error
troubleshooting
Java development

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.

Practice system design

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:

  1. Idle Connection Timeout: Connections can be closed if they remain idle for too long.
  2. Network Interruptions: Temporary network issues can lead to connection loss.
  3. Exhausted Connection Pool: If all connections are in use and not being released back to the pool.
  4. 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:

  1. Enable Debug Logging:
    • Add the following lines to your application.properties or application.yml to 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.properties file to configure HikariCP settings appropriately:
    • Ensure the maximum-pool-size is 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.