Link to home
Start Free TrialLog in
Avatar of Octalys
Octalys

asked on

500pts: When to use mysql_connect and when to use mysql_pconnect. Advantages/Disadvantages, and scenario examples. Thank you.

500pts:  When to use mysql_connect and when to use mysql_pconnect. Advantages/Disadvantages, and scenario examples. Thank you.
Avatar of Tomeeboy
Tomeeboy
Flag of United States of America image

Well, calling mysql_pconnect() will open a persistent connection that stays connected to MySQL for the length of the session (just waits for a mysql request while keeping it's connection to the database open).  Using mysql_connect() will open a new connection to the database each time it is called, but the connection does not remain open much longer than it takes for the script to execute in most cases (depending on if you close it manually and what the timeout is set at if you don't).  If there is already a connection open for the script, mysql_connect() will re-use it instead of opening a new one.

The problem I've heard with mysql_pconnect() is that it can tie up mysql server slots and cause you to reach your connection limit when many users are accessing your site.  This is because the connections are staying open for their entire session, even if they aren't querying the database.

Personally, I do not see an advantage to using mysql_pconnect() in most scenarios.  I would suggest calling mysql_connect() once at the start of your script, and then calling mysql_close() at the end of your script for the best performance.

Hope this information helps ;)
ASKER CERTIFIED SOLUTION
Avatar of Tomeeboy
Tomeeboy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Octalys, did you have any further questions or concerns about mysql_connect and mysql_pconnect?  Just curious if your questions have been answered sufficiently.  Let me know.  Thanks!
Avatar of Octalys
Octalys

ASKER

I found out that my problem was not mysql_connect or mysql_pconnect. I have 400-600 concurrent users at the same time, I needed RAM.
But mysql_connect was better than mysql_pconnect.