473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

One Global Connection vs. Open-Close

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
5 3068
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jabailo | last post by:
I created a web method to insert data on a DB2 database. It runs well, except every time I call it, it opens and closes the ADO connection. I think it would run faster, since I send it...
1
by: Colin Graham | last post by:
i am currently developing an asp.net web application which is linked to an Access database. The main problem that i have is in creating a global pathname that i can access. When i put my string...
4
by: RD | last post by:
I opened a connection to the database in the login form. Now in another form I tried to insert values to a table. But the connection is not available in the form. How can I make the connection...
4
by: Mike P | last post by:
What is the equivalent of setting a global connection string in Web.Config in WinForms? Any help would be really appreciated. Cheers, Mike
11
by: Roy Lawson | last post by:
I have no idea what is going on here. I wrote a simple application in VB.NET to generate a Crystal Report, and I am now trying to move it to ASP.NET with Crstal Enterprise. I wish I could tell...
1
by: Colin Graham | last post by:
i am currently developing an asp.net web application which is linked to an Access database. The main problem that i have is in creating a global connection string that i can use. When i put my...
2
by: breadon | last post by:
Hello there Am having problems with: Set cnn = New ADODB.Connection ........ With cnn .ConnectionString = CurrentProject.Connection .Open End With
4
by: John | last post by:
Hi I would like to open a db connection and then pass the same to all my sql commands across various pages. Is there a way to implement this? Thanks
2
by: Derek Fountain | last post by:
I've got a function that builds a webpage containing a flash animation. As the page goes to the browser, the browser sees the link to the embedded FLV file and opens a new connection to retrieve...
1
by: ErikJL | last post by:
I have a simple webservice that performs a SELECT query against a database, and then an INSERT statement on the same database/table. The problem arises at the time when we create the second OleDB...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.