Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-12-2007, 01:41 AM
|
#1
|
|
Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131
Rep: 
|
web page database access per page or per session?
As a "newbie" of one kind to dynamic web page creation and database usage, I'd like to know your opinions about this. Many examples I've seen often consist of a single file (for example php file), and thus a database connection is established and closed within the same file. Now usually dynamic web site has more than one file, but let's say I still needed to access a database from different files; would it be sensible to always, within a file (like list_some_items.php), open a database connection and once needed data is fetched, close it; then with another file (like show_some_information.php) do the same open-fetch-close-thing on the database -- or -- would it be better to establish a database connection right after a session is started for the user and remain the connection open and close it only when the user leaves, and session is destroyed?
Like I said, I'm fairly new to these things, as of now working with php and MySQL, so bear me. I'll possibly try other things in the future, but I guess this question is somewhat "common"; is there a huge difference I don't see right away, security problems having the database connection open all the time the user is in the site, maybe efficiency problems? Or is it even possible, or sane, to maintain a db connection during a session?
Thanks for opinions!
|
|
|
|
01-12-2007, 04:31 AM
|
#2
|
|
Member
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196
Rep:
|
As far as I know the open/close process to a database it is kind of slow. I mean it is slow for a couple connections each second - if your site is not traffic intensive you will probably not notice.
The best is to use connection pooling. I do not know how is this implemented in php as I used just java, but the main idea is that the server or somekind of manager opens a bunch of connections to the database and when a 'page' needs a connections it borrows it from the pool. When it doesn't need it anymore it 'release' it and the pool can reuse it.
A quick search on the web revealed this, maybe it is helpfull to you: http://www.apachetutor.org/dev/reslist
|
|
|
|
01-13-2007, 08:56 AM
|
#3
|
|
Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131
Original Poster
Rep: 
|
Thanks for the information and the link, I'll read it; this is probably what I'm after. The site isn't probably going to be too "traffic-intensive", borrowing your words, but I still somehow dislike the idea of having to open&close a separate connection every time a database-needing page is opened.
|
|
|
|
01-13-2007, 09:56 AM
|
#4
|
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,024
Rep: 
|
Hi
MySql is very fast on opening and closing connections. On a busy site, the problem is usually the number of database connections, not the time it takes. A typical scripts opens a connection, does some queries, outputs html and then closes. To scale things better, you can open the connection, do queries, close the connection and then output html.
The problem is usually slow clients keeping the connections open while getting the html. I think keeping the connection open in the session would just make things worse, and I've never seen LAMP solutions doing it.
|
|
|
|
01-13-2007, 10:28 AM
|
#5
|
|
Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131
Original Poster
Rep: 
|
Quote:
|
The problem is usually slow clients keeping the connections open while getting the html. I think keeping the connection open in the session would just make things worse, and I've never seen LAMP solutions doing it.
|
Now this sounds interesting. So you're saying it's ok to have a connection open&close once per php page (when needed), as far as the html code creation etc. are outside this? (actually this is how I always do things, never even thought the other way around)
Well, maybe there won't be speed problems since the site is and probably stays pretty small, including users that visit it..but anyway I have to do some more searching about the speed thing.
|
|
|
|
01-13-2007, 11:08 AM
|
#6
|
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,024
Rep: 
|
I think most LAMP solutions don't even consider this, they just open the database connection and it gets closed when the script is finished.
Try benchmarking your site with e.g. "ab" (Apache benchmark). I found out, putting a mysql_close() in the right place can help a lot.
|
|
|
|
01-13-2007, 12:09 PM
|
#7
|
|
Member
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196
Rep:
|
I still think there will be a performance improvement if you use connection pooling (if it is less work for the CPU it should be faster). Most of the application do not need that improvement as they run on fast servers/don't have bilions of request per seconds.
You always can test yourself  - and if you do, please post the results. I'm also curious what would be the speed difference, but probably not enough to install apache & php.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:59 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|