LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-06-2014, 06:29 AM   #1
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198
Blog Entries: 3

Rep: Reputation: 23
Database Connections


I am curious about how applications connect to databases.
For a PHP website, do you write mysql_connect for every page and
apparently request. And then call mysql_close on the same page
after the work is done. But I think this will increase the load
on the server and is less reliable.

Or do you give connection to the user when the session starts
and cleanup the connection when the user session ends. What if the
user does not end the session and just exits the browser? Will this
leave the db connection open?

The other possibilities exist like application crash and powercut.
Does it leave any stray connections to the database?

Any suggestions.
 
Old 05-06-2014, 09:25 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
For a PHP website, do you write mysql_connect for every page and apparently request. And then call mysql_close on the same page after the work is done. But I think this will increase the load on the server and is less reliable.
That's the way you typically do it, yes. I've never heard of it causing problems with either load or reliability.

It certainly won't be a problem on a single-server PHP website.

Keep in mind that with PHP, a new instance of the PHP interpreter is launched for each web request, and then closed when the request is served.

Quote:
Or do you give connection to the user when the session starts and cleanup the connection when the user session ends. What if the user does not end the session and just exits the browser? Will this leave the db connection open?
For a PHP website? The only to make that work would be to write a Java or Silverlight plugin to connect to the database. Web browsers don't support OBDC.

You'd typically clean up resources (such as open connections) when the application exits. I *assume* both Java and Silverlight allow you to detect if the browser is being closed. If not, then yes, you'd have a resource leak.

Another problem with this approach that it's not scalable. Connections (i.e. sockets) are a finite resource, and they can be used up. FTP servers, which also deal with clients with persistent connections, have the same problem.

(You could use web sockets too, but I don't think that's something you're going to go for. See: http://socketo.me/)

Quote:
Any suggestions.
Another possibility is to use the connection pool pattern. Here's the Wikipedia page: http://en.wikipedia.org/wiki/Connection_pooling

SQLAlchemy, an ORM for Python, uses connection pooling. Here's the relevant part of its developer docs:

http://sqlalchemy.readthedocs.org/en...e/pooling.html

Quote:
The other possibilities exist like application crash and powercut. Does it leave any stray connections to the database?
Yes, and dealing with that is a database administration issue. Google for, oh, "mysql time out idle connections" without the quotes.

Last edited by dugan; 05-06-2014 at 11:10 AM.
 
1 members found this post helpful.
Old 05-06-2014, 11:31 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
The HTTP protocol is stateless. You are given a request for a page, you serve that page up, and then you "fuhgeddaboudit." So, a typical CGI script does, in fact, connect to the database each and every time.

"Sessions" are a concept that is unrelated to database-connections, although a database is typically used to store session data. The objective is to, since HTTP is inherently stateless, provide a way to remember information from one page-request to the next. So, the CGI page connects to the database, retrieves the session data from it, generates the page, closes the database connection again and serves up the page.
 
1 members found this post helpful.
Old 05-06-2014, 04:15 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by kedarp View Post
Or do you give connection to the user when the session starts and cleanup the connection when the user session ends.
I realized later what you were asking here. Anyway:

http://stackoverflow.com/q/6078843

Short answer is that it's impossible.

Last edited by dugan; 05-06-2014 at 04:18 PM.
 
Old 05-07-2014, 02:22 AM   #5
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198

Original Poster
Blog Entries: 3

Rep: Reputation: 23
I thought writing mysql_connect and mysql_close on each page would produce load
on the server when around 1000 users accessing the application at the same time.

So I wanted to know it is normally done. Thanks for clearing the things.
 
Old 05-07-2014, 03:02 AM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
I thought writing mysql_connect and mysql_close on each page would produce load on the server when around 1000 users accessing the application at the same time.
It doesn't work exactly one-to-one. Optimization is done in the connection level. So as others said, it's not an issue.

However for large commercial application databases that have to correctly handle "multi statement transactions" it is necessary to keep the connection open across pages since closing the connection does an implied COMMIT WORK.

ON BALANCE, DESPITE THE POSSIBLE NEED FOR MORE MEMORY, I WOULD GO FOR A SINGLE CONNECTION (NO CLOSE IN EVERY PAGE) JUST TO ENSURE DATA ACCURACY. BELIEVE ME, IT'S MORE DIFFICULT TO CORRECT ERRORS ARISING OUT OF INCONSISTENT DATA THAN DOING IT RIGHT FIRST TIME EVEN IF IT SUPPOSEDLY TAKES MORE MEMORY.

OK

Last edited by AnanthaP; 05-07-2014 at 07:48 PM.
 
1 members found this post helpful.
Old 05-07-2014, 10:00 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by kedarp View Post
I thought writing mysql_connect and mysql_close on each page would produce load on the server when around 1000 users accessing the application at the same time.
Wouldn't having a persistent connection for each user be even more expensive, once you take memory usage into account?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] script to look for connections running in a database snooks679 Linux - General 4 07-26-2011 09:55 AM
redir for oracle database connections Felipe Linux - Networking 1 03-06-2010 04:04 PM
Web app - multiple database connections nbcohen Linux - Software 1 07-07-2009 04:44 PM
DBI Connect Failed:Access denied for user ''@'localhost' to database <database> error kaplan71 Linux - Software 3 06-05-2009 12:39 PM
Database Programming (database to database transaction) johncsl82 Programming 7 02-02-2007 08:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration