Jump to content

A few questions ..


2oMst

Recommended Posts

I have a community website and I do lots of different queries at different times in the webpage.

 

1. Is it better to open and close a MySQL connection everytime I need to gather info on the db with mysql_connect and mysql_close or to open it in the beginning and close it at the end of the page? (Knowing that i can have up to 20 group queries in one page)

 

2. If it\'s better to open it at the beginning and close it at the end, would it be better to use mysql_connect or mysql_pconnet ??

 

Could someone answer these questions mentionning why it\'s the best solution. (rendering speed, security, traffic ... )

Link to comment
Share on other sites

i will ans to my best ability atm :P ..maybe someone else can add...

 

1. Yes i believe its better to open the connection once and close it at end of script(which php does automatically anyway) ..in the case of more than 1 or 2 queries to be executed in the same script i would say yes ..use the mysql_pconnect. Of course this is all so that the server and client wouldn\'t have to re-establish connection with each other. Yes this has to do with server load/traffic..

 

2. mysql_connect or mysql_pconnect? ...not sure ...i believe it depends on ur script ...lotta queries = pconnect ...little = connect ...this is what i know so far.

 

//anyone plz correct me if needed :)

Link to comment
Share on other sites

So to summarize ..

 

holiks says it would be better for me to use pconnect since I do lot\'s of connection in the page, and there isn\'t a single page, that doesn\'t require a query.

 

For akitchin:

www.php.net says:

mysql_close() will not close links established by mysql_pconnect().

 

By reading the definition of pconnect on php.net, it looks like it works almost like a session but for MySQL but does it close if the user closes the web browser ?? Or does it stay open for other visitors ??

 

I also found this part on php.net that pretty much answers most of my questions:

If persistent connections don\'t have any added functionality, what are they good for?

 

The answer here is extremely simple -- efficiency. Persistent connections are good if the overhead to create a link to your SQL server is high. Whether or not this overhead is really high depends on many factors. Like, what kind of database it is, whether or not it sits on the same computer on which your web server sits, how loaded the machine the SQL server sits on is and so forth. The bottom line is that if that connection overhead is high, persistent connections help you considerably. They cause the child process to simply connect only once for its entire lifespan, instead of every time it processes a page that requires connecting to the SQL server. This means that for every child that opened a persistent connection will have its own open persistent connection to the server. For example, if you had 20 different child processes that ran a script that made a persistent connection to your SQL server, you\'d have 20 different connections to the SQL server, one from each child.

 

Note, however, that this can have some drawbacks if you are using a database with connection limits that are exceeded by persistent child connections. If your database has a limit of 16 simultaneous connections, and in the course of a busy server session, 17 child threads attempt to connect, one will not be able to. If there are bugs in your scripts which do not allow the connections to shut down (such as infinite loops), the database with only 16 connections may be rapidly swamped. Check your database documentation for information on handling abandoned or idle connections.

 

So if someone is browsing the webpage and idles for 3-5min, i believe that the connection will still be active, causing a +1 on the connection limit.

 

I will propably do mysql_connect at the beginning and mysql_close at the end for now.

 

If anyone can add anything on pconnect would be greatly appreciated.

 

Thank you

:D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.