Sunday, 27 May 2018

Why PHP PDO connects to different database when using persistent connection?

I connect to MySQL using PHP's PDO like this:

$driver_options[PDO::ATTR_PERSISTENT] = true;
$db = new PDO('mysql:host='.$host.';dbname='.$db_name, $user, $pass, $driver_options);

I have 2 databases (let's call them database_A and database_B) on this server and sometimes very strange thing happens. Even though, $db_name is 100% set to 'database_A', connection is made to 'database_B'.

It's happening completely random. I can run the same script 10 times over again and everything is fine. And 11th time this problem happens.

I would never expect this to happen. It gave me a lot of headache. Can anyone explain it ? And is the only solution not to use persistence ?



from Why PHP PDO connects to different database when using persistent connection?

No comments:

Post a Comment