Discussion:
Wordpress - Database persistent connection support
Rarylson Freitas
2013-08-23 16:03:10 UTC
Permalink
Hi,

I see that Wordpress uses the mysql_connect funcion in wp-db.php file. So,
the db_connect() function always return a non persistent connection to the
database.

So, I propose a patch (to Wordpress v.3.5.1).

My Idea is a new config constant in wp-config.php.

So, to enable persistente connections, the administrator will set in the
wp-config.php file:

define("WP_PCONNECT", true);

What do you think about it?

The patch is:

--- wp-db.php.old 2013-08-23 15:44:09.897307644 +0000
+++ wp-db.php 2013-08-23 15:45:33.903027939 +0000
@@ -1140,7 +1140,12 @@
if ( WP_DEBUG ) {
$this->dbh = mysql_connect( $this->dbhost,
$this->dbuser, $this->dbpassword, $new_link, $client_flags );
} else {
- $this->dbh = @mysql_connect( $this->dbhost,
$this->dbuser, $this->dbpassword, $new_link, $client_flags );
+ // Rarylson's Patch - Using pconnect
+ if ( WP_PCONNECT ) {
+ $this->dbh = @mysql_pconnect(
$this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
+ } else {
+ $this->dbh = @mysql_connect( $this->dbhost,
$this->dbuser, $this->dbpassword, $new_link, $client_flags );
+ }
}

if ( !$this->dbh ) {


--
*Rarylson Freitas
*
Computer Engineer
Otto
2013-08-23 16:08:43 UTC
Permalink
Post by Rarylson Freitas
What do you think about it?
More likely that it'll eventually just stop using the mysql extension.

http://core.trac.wordpress.org/ticket/21663

-Otto
Rarylson Freitas
2013-08-23 16:19:45 UTC
Permalink
Hi,

Excelent! PDO is better!

So, using PDO, we could make persistent connections to MySQL database,
couldn't we?

Until this next release (with #21663 ticket), I'll continue using my patch.

Thanks for the reply.

--
*Rarylson Freitas
*Computer Engineer
Post by Otto
Post by Rarylson Freitas
What do you think about it?
More likely that it'll eventually just stop using the mysql extension.
http://core.trac.wordpress.org/ticket/21663
-Otto
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Andrew Nacin
2013-08-23 16:27:49 UTC
Permalink
Post by Rarylson Freitas
I see that Wordpress uses the mysql_connect funcion in wp-db.php file. So,
the db_connect() function always return a non persistent connection to the
database.
I was discussing this with a few others this week. This is interesting, but
it isn't clear how much it would help. It is my understanding that we would
need to monitor connection limits and open connections, especially on
shared servers. It might be nice to add a flag (as proposed) but it would
make sense to figure out whether this makes sense as a default, and if not,
why not, as that in itself could be informative to the dangers of using
persistent connections.

If this had a benefit to sites on overloaded shared servers, I would think
someone (if not a hosting company) would have proposed it at some point in
the last six years. This anecdotally suggests to me that there is limited
benefit or even detriment for most sites. HyperDB also has hypothetical
persistent connection support, but it is turned off by default and I am
unsure if anyone uses it.

Interestingly, the "drawbacks" covered on
http://php.net/manual/en/features.persistent-connections.php seem far worse
for us than the giant red "warning".

So, I propose a patch (to Wordpress v.3.5.1).


Please always patch current trunk, or at worst, latest stable (3.6).
Indeed, 3.5.1 isn't even the latest stable in the 3.5 branch.

Nacin
Rarylson Freitas
2013-08-23 16:56:26 UTC
Permalink
Post by Andrew Nacin
Interestingly, the "drawbacks" covered on
http://php.net/manual/en/features.persistent-connections.php<http://php.net/manual/en/features.persistent-connections.php> seem
far worse
Post by Andrew Nacin
for us than the giant red "warning".
Yes, it's true. Basically, there was two major problems with persistent
connections.

- If the applications opens a transaction and not commit. In a non
persistent connection, there was no problem, because the query will be
commit when we call mysql_close(). But it's an application bug, and not an
error in the persistent connection method;

- If the application pooling (for example, a PHP-FPM pooling) creates more
connections than the maximum allowed in MySQL, we'll get a "Too Many
Connections" error. But it's a configuration error (a project error). In
these case, the developer must either increase your MySQL simultaneous
connection limit or uses a more sophisticated pooling method (not your
simple mysql_pconnect method).

Because of this, I proposed this option must be configurable and the
default behavior must be "persistent connections = disabled".
Post by Andrew Nacin
If this had a benefit to sites on overloaded shared servers, I would think
someone (if not a hosting company) would have proposed it at some point in
the last six years. This anecdotally suggests to me that there is limited
benefit or even detriment for most sites.
It makes sense. However, my team successfully improved applications
performance (not in Wordpress, neither in PHP, but in others systems) in
the past using persistent connections.

Maybe there was few only beneficts using persistent connections in
Wordpress, but my application shows that, using non persistent connections,
a lot of TIME_WAIT stills in the netstat output (and we're using a reduced
TIME_WAIT timeout value).
Post by Andrew Nacin
HyperDB also has hypothetical
persistent connection support, but it is turned off by default and I am
unsure if anyone uses it.
I already use WP Super Cache. Recently, I knew W3 Total Cache. Now, I'll
study about HyperDB.
Thanks for the tip!
Post by Andrew Nacin
Please always patch current trunk, or at worst, latest stable (3.6).
Indeed, 3.5.1 isn't even the latest stable in the 3.5 branch.
Thanks. If in the future, I propose some patch and it makes sense, I'll
make it based in the most recent Trunk Version.

--
*Rarylson Freitas
*Computer Enginner
Post by Andrew Nacin
Post by Rarylson Freitas
I see that Wordpress uses the mysql_connect funcion in wp-db.php file.
So,
Post by Rarylson Freitas
the db_connect() function always return a non persistent connection to
the
Post by Rarylson Freitas
database.
I was discussing this with a few others this week. This is interesting, but
it isn't clear how much it would help. It is my understanding that we would
need to monitor connection limits and open connections, especially on
shared servers. It might be nice to add a flag (as proposed) but it would
make sense to figure out whether this makes sense as a default, and if not,
why not, as that in itself could be informative to the dangers of using
persistent connections.
If this had a benefit to sites on overloaded shared servers, I would think
someone (if not a hosting company) would have proposed it at some point in
the last six years. This anecdotally suggests to me that there is limited
benefit or even detriment for most sites. HyperDB also has hypothetical
persistent connection support, but it is turned off by default and I am
unsure if anyone uses it.
So, I propose a patch (to Wordpress v.3.5.1).
Please always patch current trunk, or at worst, latest stable (3.6).
Indeed, 3.5.1 isn't even the latest stable in the 3.5 branch.
Nacin
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...