views:

226

answers:

3

I've been thinking, why does Apache start a new connection to the MySQL server for each page request? Why doesn't it just keep ONE connection open at all times and send all sql queries through that one connection (obviously with client id attached to each req)?

It cuts down on the handshake time overhead, and a couple of other advantages that I see.

It's like plugging in a computer every time you want to use it. Why go to the outlet each time when you can just leave it plugged in?

A: 

Because that's the purpose of the mod_dbd module.

Ben S
+1  A: 

MySQL does not support multiple sessions over a single connection.

Oracle, for instance, allows this, and you can setup Apache to mutliplex several logical sessions over a single TCP connection.

This is limitation of MySQL, not Apache or script languages.

There are modules that can do session pooling:

  • Precreate a number of connections
  • Pick a free connection on demand
  • Create additional connections if not free connection is available.
Quassnoi
+1  A: 

the reason is: it's simpler.

to re-use connections, you have to invent and implement connection pooling. this adds another almost-layer of code that has to be developed, maintained, etc.

plus pooled connections invite a whole other class of bugs that you have to watch out for while developing your application. for example, if you define a user variable but the next user of that connection goes down a code path that branches based on the existence of that variable or not then that user runs the wrong code. other problems include: temporary tables, transaction deadlocks, session variables, etc. all of these become very hard to duplicate because it depends on the subsequent actions of two different users that appear to have no ties to each other.

besides, the connection overhead on a mysql connection is tiny. in my experience, connection pooling does increase the number of users a server can support by very much.

longneck