noncent / pdo_class_wrapper

A Class for PDO Wrapper
25 stars 21 forks source link

SQLSTATE[08004] [1040] Too many connections #17

Closed rumplin closed 3 years ago

rumplin commented 3 years ago

When you use this class in a while or for loop, you will get errors ERROR in establish connection: SQLSTATE[08004] [1040] Too many connections

which indicates, that this class does not reuse connections, but instead opens always a new connection?

rumplin commented 3 years ago

using max_connections=1000

noncent commented 3 years ago

Hi @MokraCota,

First of all, I just wanted to say a big thank you for taking so much interest in this class wrapper and to put the efforts in the right direction.

Well, that's true because the wrapper isn't updated for a long time, you can see it passes the decade almost and haven't bit get updated yet. But now if people start taking interest in it I will certainty update the class wrapper in the most advance and in very optimized way.

to answer your question regarding the Persistent connection Yes! The good news is here and you can pass the PDO native attributes which come inbuilt with PDO to get the benefits of the persistent connections.

// use persistent connection
public function __construct( $dsn = array() ) {
.....
parent::__construct( "mysql:host=$host; dbname=$dbname", $username, $password, array(
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
    **PDO::ATTR_PERSISTENT => true**
) );
.....

I have tested the Persistent connection without a single glitch (max connection is 151 MariaDB) -

for ($i = 0; $i<=1000; $i++) {
  $db = new PdoWrapper($dbConfig);
  $rs = $db->select('s3upload')->results();
  echo str_repeat("\t", 2) . 'CON NO: ' . $i;
  echo '<pre>', print_r($rs, 1), '</pre>';
}

Much appreciate your unconditional help and support and I wish good health to you.

-- Cheers!!

rumplin commented 3 years ago

I am using this wrapper like this

in my main php script I initialize $db = new PdoWrapper($dbConfig);

then I have a few dozen $db->pdoQuery, $db->select, $db->update, etc...

then I include this main php script into my worker script php and there I again use $db as a global variable in my functions

require_once "include/main.php";

function worker($arr)
{
    global $db;
    foreach ($arr as $item) {
        $db->pdoQuery("UPDATE ...;");
    }
}

am I using this wrapper wrong? should I initiate it in each script?

rumplin commented 3 years ago

Just took a look at my class, it seems I have an old version because PDO::ATTR_PERSISTENT => true

is missing in mine. I'll update the script now and test again.

noncent commented 3 years ago

I can insert 10K+ rows in a loop without any issue. Using global vars isn't opens the new connection it's class initializing which invoke the class to create a new instance with new connections in absence of a persistence connection.

rumplin commented 3 years ago

How could I find out which script is causing these issues? I'm not an experienced PHP programmer. I have many scripts that have traffic, but I can't figure out which one is causing issues.

rumplin commented 3 years ago

Issue resolved by increasing mysql max connections value max_connections=2000

you can close this ticket