Set the connection duration when using HikariCP

Set the maximum number of seconds a connection can persist when connecting to Cloud SQL for MySQL by using the HikariCP JDBC connection pool library.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample

Java

// maxLifetime is the maximum possible lifetime of a connection in the pool. Connections that
// live longer than this many milliseconds will be closed and reestablished between uses. This
// value should be several minutes shorter than the database's timeout value to avoid unexpected
// terminations.
config.setMaxLifetime(1800000); // 30 minutes

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser