Set connection maximum lifetime

This snippet sets the maximum amount of time that a database connection may be reused. Setting a connection lifetime can help prevent errors from connections terminated by the database server or other network infrastructure, and can also help with connection load balancing.

Code sample

Go

To authenticate to AlloyDB, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// Set Maximum time (in seconds) that a connection can remain open.
db.SetConnMaxLifetime(1800 * time.Second)

What's next

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