php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71130 Allow PDO to lazy-connect
Submitted: 2015-12-15 21:26 UTC Modified: -
Votes:5
Avg. Score:4.6 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: tom at r dot je Assigned:
Status: Open Package: PDO related
PHP Version: 7.0.0 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tom at r dot je
New email:
PHP Version: OS:

 

 [2015-12-15 21:26 UTC] tom at r dot je
Description:
------------
Currently PDO connects when an instance is created `new PDO(....);` will connect to the database at the point the PDO object is created.

For performance reasons sometimes this is not required and due to the way a lot of frameworks work, will mean connecting even if a query is never made.

To overcome this, PDO is often wrapped in a class which connects at the point a query (or prepare, or other database requiring function is called. See http://stackoverflow.com/questions/23595609/is-this-how-lazy-connecting-of-a-database-connection-is-supposed-to-be and http://stackoverflow.com/questions/5484790/auto-connecting-to-pdo-only-if-needed

It would be better if PDO supported lazy connection as an option when the object is instantiated, for example:


<?php
 $dbh = new PDO($dsn, $user, $password, PDO::LAZY_CONNECT);
?>


Which would only connect to the database server when query(), prepare() or other similar functions are called.




Patches

Pull Requests

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 15:01:30 2024 UTC