This repo contains a simple sample project that demos a hang in the Cloud SQL JDBC Postgres driver. The hang happens on Windows, when Windows either sleeps or hibernates.
The issue seems to be a bug in the driver: internal timers for refreshing certificates do not trigger on time when Windows has been asleep. This causes the driver to try to create a connection with an outdated certificate, which obviously fails.
There seems to be no way to work around the issue except restarting the process.
Java 17, Gradle. Cloud SDK installed and application-default login done, authenticated as an user with permissions to connect to a Cloud SQL postgres instance. Username and password available to that database.
- Open a command prompt (cmd) in Windows.
- cd to the project directory.
- Set the database user password in the DB_PASSWORD environment variable
SET "DB_PASSWORD=<password>"
- Build a jdbc connection string for the target Cloud SQL instance. E.g.
jdbc:postgresql:///databasename?cloudSqlInstance=gcp-project:europe-west3:sql-instance-1&user=username
- Run the program, giving the connection string from the previous step as a CLI argument
gradlew app:run --args "jdbc:postgresql:///databasename?cloudSqlInstance=gcp-project:europe-west3:sql-instance-1&user=username"
- The program will connect and start periodically checking the connection status. If the connection fails, a reconnection is attempted.
- Put the Windows PC to sleep and let it be for at least an hour.
- Wake the computer after the ephemeral certs have expired (1 hour). The logs will show that the connection has failed during sleep (of course), but the reconnection also fails.
- If the program is left alone for about an hour (depending on how quickly the PC was put to sleep after the program started), the connections should eventually recover as the JDBC library finally refreshes the certs.