a database connection object is a resource, meaning it's something magical from outside the php world (afaik a c struct or object). you can't serialize a resource, and storing a variable in the session means serializing it (the same thing serialize();
does).
php has a "nothing shared" nature, meaning it shares no resources with other requests. 2 requests may both be from user A, or one user A and one user B, no way to know. every request is encapsulated, and the only very limited "workaround" to make a logical connection between two requests are sessions. everything else must be grabbed from the outside (databases, files, memcached), so it's not php's problem anymore.
because of that, sharing database resources between requests was never meant to be. for every request his own mysql_connect. i assume this was the main reason making mysql popular with the php community in the first place. mysql connects a lot faster than other rdbms, because it had less features (no transactions, no views, no triggers, no nothing), so it was a good fit.
mysql_pconnect did connection pooling (but on a different level), but it wasn't a magic wand either. nowadays you should use PDO anyway (and PDO doesn't support it afaik).
if your application is to slow, the problems lie elsewhere.
my advice:
- whatever you think you do ... you're not sharing connections, so have a look at that. in fact, your application may be slow because of your trick (if it creates the connection for every query). remove that feature and test again.
- check your queries (
EXPLAIN SELECT * FROM foo;
)
- get your indices right
- use caching (eg. memcached)
The most common performance problem I see is people who think there's a most-common performance problem that they should be looking for, instead of measuring to find out what their actual performance problem actually is.
- cary millsap