Jump to content


Photo

A few questions ..


  • Please log in to reply
4 replies to this topic

#1 2oMst

2oMst
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationFrance, Cannes

Posted 20 July 2003 - 08:32 PM

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 ... )
The technology is here guys ! use it and make us readers, life more simple.
[br][php] your php scripts !!! (even if just 1 line)
[br][code=auto:0] is not for php, php = [php]
[br]ENTER key is to go to the line or skip a line. Like when you\'re trying to make a point or talking about something else.
[br]Thank you ;)

#2 holiks

holiks
  • Members
  • PipPipPip
  • Advanced Member
  • 83 posts

Posted 21 July 2003 - 12:45 PM

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 :)
__________________________________________________________[br]$php = array('..a stack', '...once you pop', '...you don't stop');

#3 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 21 July 2003 - 01:14 PM

I dont recommentd pconnect because you have to explicitly close the connection when it its not needed.

When using the general connect its not really compulsary to do that.
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 21 July 2003 - 01:20 PM

and just for mention, does

mysql_close($connection)

actually close persistent connections? i read somewhere that it doesn\'t, but if that\'s the case, how do you close the connection?

#5 2oMst

2oMst
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationFrance, Cannes

Posted 21 July 2003 - 04:11 PM

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
The technology is here guys ! use it and make us readers, life more simple.
[br][php] your php scripts !!! (even if just 1 line)
[br][code=auto:0] is not for php, php = [php]
[br]ENTER key is to go to the line or skip a line. Like when you\'re trying to make a point or talking about something else.
[br]Thank you ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users