Persistent connection using PDO

Source: Internet
Author: User
Tags odbc connection php website

Regardless of the programming language, almost always have to deal with a variety of databases. However, it is well known that the connection between the program and the database is a resource-intensive thing, so many experts and predecessors in the field of programming technology envision and propose various solutions to reduce unnecessary waste of resources, thereby increasing the access of the program to the database.

Among the many connectivity scenarios for programs and databases, the most well-known and widely used solution is the use of database connection pooling. However, the main character we are going to introduce today is not the connection pool, which is a bit of a hassle after all to write code to create a connection pool or use a third-party existing connection pool solution. In PHP, we can also use a more straightforward database connection scheme--pdo persistent connections.

About PDO itself, there is no more introduction, you can refer to the previous article "using PDO to connect to a variety of databases" and the official PHP website on the relevant information.

The use of the PDO persistent connection is very simple. As an example of the code in the article, "Using PDO to connect to multiple databases," we use the PDO persistent connection to access the database in the following code:

<?php
$type = ' MySQL '; Database type
$db _name = ' Test '; Database name
$host = ' 127.0.0.1 ';
$username = ' root ';
$password = ' ready ';

$DSN = "$type: host= $host;d bname= $db _name";
try {
Establish a persistent PDO connection
$pdo = new PDO ($DSN, $username, $password, array (pdo::attr_persistent = true));
} catch (Exception $e) {
Die (' Connect database failed! ');
}
$stmt = $pdo->query (' SELECT ID, body from demo ');
while ($row = $stmt->fetch ()) {
echo "Id= $row [0],body= $row [1]<br/>";
}
$pdo = null;
?>

As you can see from the code above, establishing a PDO persistence connection requires only the value of the key name Pdo::attr_persistent in the 4th parameter of the PDO constructor (the parameter is an optional parameter, which is an array containing multiple drive option parameters) True. In addition, they are used in a way that is consistent with non-persistent connections.

In PHP, we create a database connection using the new PDO () statement, and when the connection to the database succeeds, you will get an instance of the PDO class that remains active for the lifetime of the PDO object. To close the connection, you need to destroy the object to ensure that all references remaining to it are deleted, and you can assign a null value to the object variable. If you do not do this explicitly, PHP will automatically close the connection at the end of the script.

However, when we create a data connection for the PDO persistent connection, the connection is used or the PHP script ends and is not closed, but is cached by PHP. When another PHP script requests a connection that uses the same credentials (host, port, database name, user name, password, and so on), PHP will return directly to the previously cached connection to make the connection reusable. Persistent connection caching avoids the overhead of creating a new connection each time the database is accessed, making the Web application faster.
Attention

If you want to use persistent connections, you must set pdo::attr_persistent in the drive option array passed to the PDO constructor. If this property is set with Pdo::setattribute () After object initialization, the driver will not use persistent connections.

If you use the PDO ODBC Driver and the ODBC Library supports ODBC connection pooling (there are UNIXODBC and Windows two practices; there may be more), it is recommended that you do not use persistent PDO connections, but instead leave the connection cache to the ODBC connection pool layer processing. The ODBC connection pool is shared with other modules in the process, and if a PDO persistent connection is used, the connection is never returned to the ODBC connection pool, resulting in additional connections being created to serve other modules.

Persistent connection using PDO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.