CodexBloom - Programming Q&A Platform

GCP Cloud SQL connection pooling implementing Spring Data JPA causing transient connectivity errors

👀 Views: 11 💬 Answers: 1 📅 Created: 2025-06-09
google-cloud-platform spring-boot cloud-sql Java

Hey everyone, I'm running into an issue that's driving me crazy. I'm working on a personal project and I'm experiencing intermittent connectivity issues with my Spring Boot application when trying to connect to Cloud SQL using Spring Data JPA. Despite following the official documentation, I'm working with the behavior message `org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure` during peak load times. I have configured my application properties for connection pooling as follows: ```properties spring.datasource.url=jdbc:mysql://<CLOUD_SQL_CONNECTION_NAME>/<DATABASE_NAME> spring.datasource.username=<USERNAME> spring.datasource.password=<PASSWORD> spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.maximum-pool-size=20 spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.idle-timeout=600000 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true ``` I've implemented HikariCP for connection pooling but noticed that during peak loads, I often receive the aforementioned `Communications link failure` errors, leading to service disruption. I've also tried increasing the `maximum-pool-size` to 40, but it hasn’t alleviated the scenario. My Cloud SQL instance is set to allow a maximum of 200 connections, so I don't believe I’m hitting that limit. Additionally, I've added some logging to track the connection lifecycle, and I see that connections are being created and closed rapidly. When the failures occur, I can see in the logs that connections remain open for a long time, which might indicate some are not being released back to the pool promptly. I’m using Spring Boot version 2.5.4 and MySQL driver version 8.0.26. Is there a recommended way to handle these transient connectivity issues more gracefully? Should I consider adjusting my connection pool settings further or perhaps tweaking the Cloud SQL instance itself? Any insights or best practices would be greatly appreciated. This is part of a larger application I'm building. I'm on Windows 10 using the latest version of Java. This is happening in both development and production on Windows 11. What are your experiences with this?