473,624 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3090
"john" <pu*****@gmail. comwrote in message
news:11******** **************@ y80g2000hsf.goo glegroups.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.goo glegroups.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 "applicatio n".

In the context of the stateless http protocol there is no "applicatio n".
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*******@attgl obal.net
=============== ===
Apr 29 '07 #4
On Apr 29, 9:48 am, "rf" <r...@invalid.c omwrote:
"john" <puop...@gmail. comwrote in message

news:11******** **************@ y80g2000hsf.goo glegroups.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 "applicatio n".

In the context of the stateless http protocol there is no "applicatio n".
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
389
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 hundreds of records, if I could just keep a connection open at the point the web service starts up and close it when the web service is shut down ( so I would open it in the Application_Start and close it in the Application_End ).
1
3790
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 into either the appsettings or a global settings module i get an error on the server.mappath part of the string. how can i get round this as i dont want to have to use a specific path for the web application. it seems that this should be easy as...
4
9327
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 global to all forms? Where I can declare global variables?( Like in VB standard module) Thanks RD
4
5245
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
3109
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 you how the report works...but I can't even get past connecting to the database :-) I use the typical: Dim oConnection As New SqlConnection(strCon) Dim cmdText As String = "SELECT * FROM viewLogin WHERE " & _
1
5063
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 string into either the appsettings or a global settings module i get an error on the server.mappath part of the string. how can i get round this as i dont want to have to use a specific path for the web application. it seems that this should be...
2
1615
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
1807
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
1389
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 it. A moment later the page download is completed, and a few seconds after that, depending on the user's connection speed, the FLV download completes and the use has the content. All fine. The issue comes if the user receives the page, but then...
1
4590
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 connection object and attempt to Open() it. The connection string is exactly the same for both connection objects. Another intersting thing is that this code works fine when it is used in a Windows Form project. The code snippet: try { //...
0
8170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.