0

I have website which uses database, I have implemented singleton pattern to connect to the database.

But my website uses lots of Iframe or inner pages which in turn connects to database using a singleton pattern in each page.

I have few questions to be clarified 1)if a user A comes to website, he browses 10 pages, does that for all the pages only one connection is created or for every page one connection is created?

2)For all the DB connections amde in the sub pages will use the same connection or a new one ?

3)When the user exists from the website, how to close the DB Connection? I am finding tricky to close the DB Connection, Suppose in my index.php, if it has menu.php, stats.php and profile.php, I am not able to make out when to close the connection? What is the best pratice to close the connection?

Website is build on PHP and MYSQL database.

gmhk
  • 15,598
  • 27
  • 89
  • 112

2 Answers2

1

If you use a singleton, then the connection is established once for every request.

  • For every of the 10 pages.
  • If you include your sub pages via iframes, then you're opening a new connection for every sub page.
  • One option would be to have a single entry point for your webpage (a front controller) and then closing the connection, after the request has been handled. If you include your php pages in your index.php file, then close the connection after the include/require statements.
mAu
  • 2,020
  • 1
  • 14
  • 27
  • 2
    [right, and thats why Singletons are not as unique in PHP as one might think](http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323) – Gordon Oct 18 '11 at 15:48
  • Above is the Sample code that is been placed in all the pages of my website www.cricandcric.com and as per your suggestion do i need to place closing connection statement after require
    – gmhk Oct 18 '11 at 15:49
  • Close the connection after your last `require` in the file containing the `require` statements. – mAu Oct 18 '11 at 15:52
  • php automatically closes the connections when the script ends – andho Oct 18 '11 at 15:56
  • I read some article, Does PHP based web application does not require the DB Close connection manually, Server closes them automatically. is that true? – gmhk Oct 18 '11 at 15:58
  • What is the best practice to include the Subpages in the main page, by not allowing to create a new connection, instead of the old connection to be re used? in my case Index.php uses sub pages, i want to avoid to creating a new connection, instead use my old connection – gmhk Oct 18 '11 at 16:00
  • @andho: it is good practice to close the connection manually. – mAu Oct 18 '11 at 16:04
0

Some developers believe it is good practice to call for a close of connection at the end of their pages. Some of them also spend time freeing variables and arrays at the end of a script. To me this is a waste of bytes. It's ok to do things just because they are good coding practice but in my opinion, we should be aiming for fast loading web pages and the efficient use of databases in the exchange of data (to and from). The fact is, when you close a php script or navigate to another page, unless both pages are using php sessions then the previous page and all that was on it will die. It no longer exists and php dumps it.

There is no way to retrieve any of that information once this happens as it is truly killed. As for database connections, they close themselves.

The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close(). This is stated in the manual under mysql_connect();

Once again, there are those that like to put a line of code at the end of their scripts calling mysql_close($DB) but remember the waste of bytes thing?

If it is already being done, leave it along. Just my 2 cents worth.

Mark
  • 1
  • 1
    I disagree with you: Since the connection will be closed anyway, calling the `close` explicitly is very little waste: a single line of code in fact, which will be executed anyway. So it's really just the actual bytes that make up the text of the code, not even the bytes that make up the function call. It's minimal. And it isn't even bandwidth; just a dozen bytes on the server's hard drive. If you're that hard up for bytes, then you need a bigger disk. Also, not everything does get closed automatically. MySQL connections in PHP, sure, you're safe; but one day this attitude will bite you. – Spudley Apr 22 '12 at 19:46