When building high-concurrency web applications, the overhead of database connections becomes a significant performance bottleneck. Re-establishing a database connection for every request wastes resources and can lead to frequent disconnections. To solve this, PHP provides a mechanism for persistent database connections. The connect() function, available in some extensions like MySQL and PostgreSQL, can be used persistently to improve system performance and stability.
A persistent connection means that the PHP script does not close the database connection after the request ends. Instead, it keeps the connection alive in a connection pool for future reuse. This avoids the repeated “connect-disconnect-reconnect” cycle and reduces server load.
Using MySQL as an example, PHP’s mysqli and PDO extensions can both be configured to use persistent connections. Here's a sample using mysqli:
<?php
$host = 'p:localhost'; // Note the 'p:' prefix indicates a persistent connection
$user = 'db_user';
$password = 'db_pass';
$database = 'example_db';
<p>$conn = new mysqli($host, $user, $password, $database);</p>
<p>if ($conn->connect_error) {<br>
die("Connection failed: " . $conn->connect_error);<br>
}</p>
<p>echo "Persistent connection established!";<br>
?><br>
In the code above, by adding the p: prefix to the hostname, mysqli attempts to establish a persistent connection. If a reusable connection is available in the pool, it is reused; otherwise, a new connection is created.
PDO offers a cleaner way to enable persistent connections by simply setting an option:
<?php
$dsn = 'mysql:host=localhost;dbname=example_db';
$user = 'db_user';
$password = 'db_pass';
<p>$options = [<br>
PDO::ATTR_PERSISTENT => true,<br>
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,<br>
];</p>
<p>try {<br>
$pdo = new PDO($dsn, $user, $password, $options);<br>
echo "PDO persistent connection established!";<br>
} catch (PDOException $e) {<br>
echo "Connection failed: " . $e->getMessage();<br>
}<br>
?><br>
Using PDO::ATTR_PERSISTENT makes it very easy to enable persistent connections without modifying the host address.
Performance Boost: Avoids the CPU and memory overhead of repeatedly creating connections.
Faster Response: Since the connection already exists, database operations can proceed almost instantly.
Resource Reuse: Multiple requests can share connection resources, reducing pressure on the database server.
While persistent connections offer many benefits, there are also potential issues to be aware of:
Risk of Connection Leaks: If the code does not properly handle transactions or close statements, resources may accumulate within the connection.
Connection State Contamination: Since connections are reused across requests, it’s essential to clean the connection state at the beginning of each request.
Maximum Connection Limit: Persistent connections are not easily released and may exhaust the database’s maximum allowed connections. Proper configuration at the database level is recommended.
Assuming the remote database address is db.m66.net, and you want to use PDO for a persistent connection:
<?php
$dsn = 'mysql:host=db.m66.net;dbname=example_db';
$user = 'remote_user';
$password = 'remote_pass';
<p>$options = [<br>
PDO::ATTR_PERSISTENT => true,<br>
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,<br>
];</p>
<p>try {<br>
$pdo = new PDO($dsn, $user, $password, $options);<br>
echo "Successfully connected to the remote database!";<br>
} catch (PDOException $e) {<br>
echo "Connection failed: " . $e->getMessage();<br>
}<br>
?><br>
By using the p: prefix with mysqli, or enabling PDO::ATTR_PERSISTENT in PDO, PHP developers can easily implement persistent database connections. While this approach significantly boosts performance and response time, it must be paired with good resource management practices to ensure connection safety and clean state. Properly monitoring database connection counts and application health is key to maintaining a stable system.