Connect efficiently to MySQL Database on Azure

Database connections are a limited resource, so making sensible use of connection pooling to access MySQL Database on Azure optimizes performance. This article explains how to use connection pooling or persistent connections to more effectively access MySQL Database on Azure.

Access databases by using connection pooling (recommended)

Managing database connections can have a significant impact on the performance of the application as a whole. To optimize the performance of your program:Managing database connections can have a significant impact on the performance of the application as a whole.To optimize the performance of your program, the goals should be to reduce the number of times connections are established and to avoid putting the time for establishing connections in key code paths.We strongly recommend that you use database connection pooling or persistent connections to connect to MySQL Database on Azure. Database connection pooling handles the creation, management, and allocation of database connections. When a program requests a database connection, it prioritizes the allocation of existing idle database connections, rather than the creation of a new connection.After the program has finished using the database connection, the connection is recovered in preparation for further use, rather than simply being closed down.

For better illustration, this article provides a piece of sample code that uses JAVA as an example. For more information, see Apache common DBCP.

Note

The server configures a timeout mechanism, closing a connection that has been in an idle state for some time to free up resources. Be sure to set up the authentication system to ensure the effectiveness of persistent connections when you are using them. For more information, see How to configure authentication systems on the client side to ensure the effectiveness of persistent connections

Access databases by using persistent connections (recommended)

Use persistent connections in PHP. The concept of persistent connections is similar to that of connection pooling. It is important to note that PHP currently has three types of drivers. Although the MySQLi driver type does not support persistent connection, the other two types of drivers do.

For more information, Using PDO to create persistent connections and Using the MySQL engine to create persistent connections.

Replacing short connections with persistent connections requires only minor changes to the code, but it has a major effect in terms of improving performance in many typical application scenarios.

Access databases by using wait and retry mechanisms with short connections

If you have resource limitations, we strongly recommend that you use database pooling or persistent connections to access databases.If you use short connections and experience connection failures when you approach the upper limit on the number of concurrent connections, you can try connecting multiple times. You can set an appropriate wait time, with a shorter wait time after the first attempt. Thereafter, you can try waiting for random events multiple times.