This page provides best practices and language-specific code samples to help you create applications that use Cloud SQL database connections effectively.
These samples are excerpts from a complete web application available to you on GitHub. Learn more.
For step-by-step instructions on running a sample web application connected to Cloud SQL, follow the link for your environment:
A connection pool is a cache of database connections that are shared and reused to improve connection latency and performance. When your application needs a database connection, it borrows one from its pool temporarily; when the application is finished with the connection, it returns the connection to the pool, where it can be reused the next time the application needs a database connection.
Connect with TCP Connect with Unix sockets Open and close connectionsWhen you use a connection pool, you must open and close connections properly, so that your connections are always returned to the pool when you are done with them. Unreturned or "leaked" connections are not reused, which wastes resources and can cause performance bottlenecks for your application.
Python Java Node.js C# Go Ruby PHP Note: Most PHP applications are not long-running. They start at the beginning of request processing and shut down when request processing ends. Database connections are closed when the application shuts down. Connection countEvery database connection uses client and server-side resources. In addition, Cloud SQL imposes overall connection limits that cannot be exceeded. Creating and using fewer connections reduces overhead and helps you stay under the connection limit.
Python Java Node.js C# Go Ruby PHPPDO currently doesn't offer any functionality to configure connection limits.
Exponential backoffIf your application attempts to connect to the database and does not succeed, the database could be temporarily unavailable. In this case, sending repeated connection requests wastes resources. It is preferable to wait before sending additional connection requests in order to allow the database to become accessible again. Using an exponential backoff or other delay mechanism achieves this goal.
This retry only makes sense when first connecting, or when first grabbing a connection from the pool. If errors happen in the middle of a transaction, the application must do the retrying, and it must retry from the beginning of a transaction. So even if your pool is configured properly, the application might still see errors if connections are lost.
Python Java Node.js C# GoThe database/sql package currently doesn't offer any functionality to configure exponential backoff.
Ruby PHPPDO currently doesn't offer any functionality to configure exponential backoff.
Connection timeoutThere are many reasons why a connection attempt might not succeed. Network communication is never guaranteed, and the database might be temporarily unable to respond. Make sure your application handles broken or unsuccessful connections gracefully.
Python Java Node.js C# GoThe database/sql package currently doesn't offer any functionality to configure connection timeout. Timeout is configured at the driver level.
Ruby PHP Connection durationLimiting a connection's lifetime can help prevent abandoned connections from accumulating. You can use the connection pool to limit your connection lifetimes.
Python Java Node.jsThe 'knex' Node.js library currently doesn't offer any functionality to control the duration of a connection.
C# Go RubyActiveRecord currently doesn't offer any functionality to control the duration of a connection.
PHPPDO currently doesn't offer any functionality to control the duration of a connection.
View the complete applicationTo see the complete application, click the link below.
What's nextRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4