7

I'm trying to improve performance on a volkszaehler.org implementation by enabling persistent DB connections. Having hacked included Doctrine's Connection class to have PDO::ATTR_PERSISTENT => true, I'm getting the PDO error General error: PDO::ATTR_STATEMENT_CLASS cannot be used with persistent PDO instances"

Is there any way to fix this?

andig
  • 13,378
  • 13
  • 61
  • 98
  • Is it broken? What is wrong with the standard way of connection pooling PDO offers? Or just scratching an itch? – hakre Apr 25 '13 at 15:17
  • I've seen the PDO connect even to localhost take 1000ms and was investigating if persistent connections couldn't cure this. – andig Apr 25 '13 at 15:58
  • Use mysql native driver. look into the stats. that might give you better insights what is going on before jumping cross. [How to know if MySQLnd is the active driver?](http://stackoverflow.com/q/1475701/367456) and http://php.net/manual/it/mysqlnd.stats.php – hakre Apr 25 '13 at 16:02
  • Unfortunately Doctrine works on PDO only? – andig Apr 25 '13 at 19:28

1 Answers1

16

You could pass your own PDO instance to Doctrine, setting the persistent connection yourself:

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
    PDO::ATTR_PERSISTENT => true
));

$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
    'dbname' => 'mydb',
    'user' => 'user',
    'password' => 'secret',
    'host' => 'localhost',
    'pdo' => $dbh,
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);

Be sure to know the implications of using persistent connections with PDO: What are the disadvantages of using persistent connection in PDO

Community
  • 1
  • 1
bspellmeyer
  • 783
  • 5
  • 10
  • How would that take care of the error message? My problem is not that I don't know how to create a persistent connection??? – andig Apr 25 '13 at 15:59
  • 1
    Did you actually try my solution? As it does not use Doctrines own PDOConnection it should not actually set the PDO::ATTR_STATEMENT_CLASS, and thus your error should be gone. – bspellmeyer Apr 25 '13 at 16:12
  • I'm sorry, I didn't as I didn't realize the connections. It does indeed work, thanks a lot! Update: could you update the anser so I can upvote? – andig Apr 25 '13 at 18:35
  • This has now significantly (0.5s instead of 1.5sec per request) helped the performance on my dev machine. Unfortunately it doesn't help on production. I'm suspecting that the prod machine doesn't allow persistent connections (?) but have no idea how to test/ which config to tweak. Anyway, great answer, thanks! – andig Apr 25 '13 at 18:50