-
☆ A M B ☆
- 24,524 Posts
I know this isn’t really xPDO, but I’ve been posting all over the internet and nobody seems to have any idea. What does PDO do if it’s instantiated when there’s already an existing (persistant) connection? Does it just create a new connection, or does it detect the redundancy and re-use the existing one?
I’ve discovered that MODx isn’t bothered at all by a snippet with a PDO object started up and used; you can do your PDO stuff and use MODx API functions interchangeably in the same snippet.
It just seemed to me that if it’s creating its own protected connection, then maybe PDO and MODx (Evo) wouldn’t be such a great idea, since it’s sucking up the resources of two connections. About the only obvious benefit I can see (other than making your snippet able to use different database engines, which would not cause the above database load) is if you wanted to use prepared statements in processing large loops of data. Then the question would be, is the trade-off between having two connections opened and streamlining the repeated processing of the data in the loop to the good?
-
MODX Staff
- 10,725 Posts
Each instance of a PDO object has it’s own connection.
Connections to the database can be the most expensive operation that occurs in each PHP execution, so you are correct in thinking that using PDO in a system that already has a connection via the mysql extension (or mysqli, or any native db extension) would now double the connection time and use about twice the resources. I have used xPDO in several Evolution projects for the development/maintenance benefits, and it performs within reason, though I do bypass it in some front-end code (opting to use the DBAPI to query the data directly) to avoid the extra overhead where it’s important. But if you are going for maximum performance, unnecessary connections would probably be the first thing to be sacrificed on my list.