By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,724 Members | 1,868 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,724 IT Pros & Developers. It's quick & easy.

One Global Connection vs. Open-Close

P: n/a
All:

I have a MS background and am having fun in the open source world of
PHP and MySQL --- and I have a question.

With PHP and MySQL, is it considered better practice to open a single
database connection and re-use it throughout the life of the
application (simple Web application - low traffic), or is it better to
open a connection, use it and close it as needed?

(With SQL Server, you'd do the latter, because connection pooling and
other optimization mechanisms help ensure that connecting to the
database is low cost.)

Thanks,
John
jpuopolo

Apr 29 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"john" <pu*****@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
All:

I have a MS background and am having fun in the open source world of
PHP and MySQL --- and I have a question.

With PHP and MySQL, is it considered better practice to open a single
database connection and re-use it throughout the life of the
application (simple Web application - low traffic), or is it better to
open a connection, use it and close it as needed?

(With SQL Server, you'd do the latter, because connection pooling and
other optimization mechanisms help ensure that connecting to the
database is low cost.)
The latter is actualy very fast in php too. Surprisingly so.
Vince
Apr 29 '07 #2

P: n/a
rf

"john" <pu*****@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
All:

I have a MS background and am having fun in the open source world of
PHP and MySQL --- and I have a question.

With PHP and MySQL, is it considered better practice to open a single
database connection and re-use it throughout the life of the
application (simple Web application - low traffic), or is it better to
open a connection, use it and close it as needed?
Define "application".

In the context of the stateless http protocol there is no "application".
There are only individual seperate request to get a specific page at a
specific URL.

--
Richard.
Apr 29 '07 #3

P: n/a
john wrote:
All:

I have a MS background and am having fun in the open source world of
PHP and MySQL --- and I have a question.

With PHP and MySQL, is it considered better practice to open a single
database connection and re-use it throughout the life of the
application (simple Web application - low traffic), or is it better to
open a connection, use it and close it as needed?

(With SQL Server, you'd do the latter, because connection pooling and
other optimization mechanisms help ensure that connecting to the
database is low cost.)

Thanks,
John
jpuopolo
(Hit send too fast)

As for connection pooling - yes, I've seen this being recommended in SQL
Server also - but I've found it's only good when you have a lot going on
- same with PHP and MySQL.

The reason is - with connection pooling, you are allocating server
resources all the time - whether they are needed or not. And you have
to allocate at least as many resources as you may *ever* need - and
should allocate more, just as a buffer. So, fir instance, if you at
some peak time you might need 200 connections concurrently, you need 200
connections in the pool all the time.

It's more important with SQL Server because it takes longer to connect
to the server. MySQL is a lot more efficient in that respect, and while
under certain conditions pooling (or persistent connections) might help,
you won't find the gain you do with SQL Server.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 29 '07 #4

P: n/a
On Apr 29, 9:48 am, "rf" <r...@invalid.comwrote:
"john" <puop...@gmail.comwrote in message

news:11**********************@y80g2000hsf.googlegr oups.com...
All:
I have a MS background and am having fun in the open source world of
PHP and MySQL --- and I have a question.
With PHP and MySQL, is it considered better practice to open a single
database connection and re-use it throughout the life of the
application (simple Web application - low traffic), or is it better to
open a connection, use it and close it as needed?

Define "application".

In the context of the stateless http protocol there is no "application".
There are only individual seperate request to get a specific page at a
specific URL.

--
Richard.
My definition of an application in this context is: a set of Web pages
and Web-based, server-side resources that act as a semantic or logical
unit, whose state is tied together for some period of time using a
session-based mechanism.

Apr 29 '07 #5

P: n/a
On Apr 29, 2:56 pm, john <puop...@gmail.comwrote:
All:

I have a MS background and am having fun in the open source world of
PHP and MySQL --- and I have a question.

With PHP and MySQL, is it considered better practice to open a single
database connection and re-use it throughout the life of the
application (simple Web application - low traffic), or is it better to
open a connection, use it and close it as needed?

(With SQL Server, you'd do the latter, because connection pooling and
other optimization mechanisms help ensure that connecting to the
database is low cost.)

Thanks,
John
jpuopolo
It's not so much a better practice as the only practice: In PHP, you
can't create a connection that last beyond the current request. So you
have to reestablish the connection everytime. There is this feature
called persistent connection in PHP but it should be avoided like the
plague. It has a way of leading to connection failures and the saving
is negligible when the database is hosted on the same server.

Even without connection pooling, one should close the connection as
soon as it isn't need as there's a limit to the number of concurrent
connections in MySQL.

Apr 30 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.