1

As in title, when creating multiple PDO objects with the same DSN parameters (username, hostname, password etc.) - would all instances have the same link to the database under the hood? Of course they would all be different instances of PDO, but on the lower level, is the link to the database associated with all of them the same?

That's how it works when calling mysql_connect() multiple times with the same parameters would return the same link would do if called with the same parameters.

http://uk1.php.net/mysql_connect

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.

As requested updating with an example...

$con_1 = new PDO($db_type . ':host=' . $db_host . ';dbname=' . $db_name, $db_user, $db_pass);
$con_2 = new PDO($db_type . ':host=' . $db_host . ';dbname=' . $db_name, $db_user, $db_pass);

Does $con_1 and $con_2 have the same link to the actual database?

UPDATE Removed the benchmark as $db was getting overwritten.

user2723025
  • 477
  • 4
  • 16
  • An example of code you are trying please. – Sidstar Mar 20 '14 at 00:02
  • 3
    They're only re-used if you set the `PDO::ATTR_PERSISTENT` constructor attribute to `true`. See http://www.php.net/manual/en/pdo.connections.php#example-906 – Phil Mar 20 '14 at 00:04
  • @Phil Post that as an answer. – Barmar Mar 20 '14 at 00:07
  • @Barmar it would just be a link to the manual. In my opionion, RTFM is not an appropriate answer for StackOverflow unless the manual page in question is poorly written and needs further explanation. – Phil Mar 20 '14 at 00:08
  • @Phil, yes, but then isn't making persistent database connection asking for trouble? As per the manual they "are not closed at the end of the script" - I would like to close them at the end of the script, however if requested again during the same request lifecycle, I would like to have it reuse the existing connection... database connections are supposed to be short-lived however it does make sense to reuse it within the request lifecycle... and close at the end of the script – user2723025 Mar 20 '14 at 00:10
  • @user2723025 There's a nice discussion over here - [What are the disadvantages of using persistent connection in PDO](http://stackoverflow.com/questions/3332074/what-are-the-disadvantages-of-using-persistent-connection-in-pdo) – Phil Mar 20 '14 at 00:15
  • @Phil, thanks for the link, please see my benchmark "discovery" – user2723025 Mar 20 '14 at 00:45
  • @user2723025 Could be because you're overwriting the `$db` variable each time. That might be closing the connection when the reference is garbage collected. For a real test, I'd try adding them to an array, eg `$db[] = new PDO(...)` – Phil Mar 20 '14 at 00:48
  • Factory pattern is the solution. A Factory class that returns my instance of PDO, cached in a private static. – user2723025 Mar 20 '14 at 02:11

1 Answers1

1

No, by default PDO will open a new connection - even when you call it with the same dsn (and credentials)

but check @Phil's comment below your question. (thanks! :) ... You can pass PDO::ATTR_PERSISTENT along with the $attributes argument of the constructor and set it to true if you want to reuse the connection.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266