how to do persistent database connection in wordpress

how to do persistent database connection in wordpress

Here in wordpress tutorial, we explained, how to do persistent database connection in wordpress. Persistent connections are links that do not close when the execution of your script ends.

What is persistent database connection?

Persistent connections are links that do not close when the execution of your script ends. When a persistent connection is requested, PHP checks if there’s already an identical persistent connection (that remained open from earlier) – and if it exists, it uses it. If it does not exist, it creates the link. An ‘identical’ connection is a connection that was opened to the same host, with the same username and the same password (where applicable).

how to do persistent database connection in wordpress
how to do persistent database connection in wordpress

If you want to use the persistent database connection then you should follow my steps:

how to do persistent database connection in wordpress

First Open the wp-db.php file from wp-includes folder. In that file find following words:


// @mysql_connect( $dbhost, $dbuser, $dbpassword, true );

//Change that to

@mysql_pconnect( $dbhost, $dbuser, $dbpassword, true );

comment the mysql_connect line. This line you will find two times in that file. You need to change the line both the times. Then upload this file to your wordpress installation.

Persistent database connection will open only one connection and for every query that will check for connection is present or not. If connection is already present then your query will execute using that persistent database connection.

There are couple of issues with persistent database connection, When you are using the persistent connection you should keep following things in mind.

Imp: There are a couple of additional caveats to keep in mind when using persistent connections. One is that when using table locking on a persistent connection, if the script for whatever reason cannot release the lock, then subsequent scripts using the same connection will block indefinitely and may require that you either restart the httpd server or the database server. Another is that when using transactions, a transaction block will also carry over to the next script which uses that connection if script execution ends before the transaction block does. In either case, you can use register_shutdown_function() to register a simple cleanup function to unlock your tables or roll back your transactions. Better yet, avoid the problem entirely by not using persistent connections in scripts which use table locks or transactions (you can still use them elsewhere).

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

3 thoughts on “how to do persistent database connection in wordpress”

Leave a Reply

Your email address will not be published.