PHP mySQL - When is the best time to disconnect from the database?

前端 未结 6 596
抹茶落季
抹茶落季 2020-12-03 17:08

I use lazy connection to connect to my DB within my DB object. This basically means that it doesn\'t call mysql_connect() until the first query is handed to it, and

相关标签:
6条回答
  • 2020-12-03 17:35

    Using a lazy connection is probably a good idea, since you may not need the database connection at all for some script executions.

    On the other hand, once it's open, leave it open, and either close it explicitly as the script ends, or allow PHP to clean up the connection - having an open connection isn't going to harm anything, and you don't want to incur the unnecessary overhead of checking and re-establishing a connection if you are querying the database a second time.

    0 讨论(0)
  • 2020-12-03 17:37

    You may want to look at a using persistent connections. Here are two links to help you out

    http://us2.php.net/manual/en/features.persistent-connections.php

    http://us2.php.net/manual/en/function.mysql-pconnect.php

    0 讨论(0)
  • 2020-12-03 17:42

    As far as I know, unless you are using persistent connections, your MySQL connection will be closed at the end of the page execution.

    Therefore, you calling disconnect will add nothing and because you do the lazy connection, may cause a second connection to be created if you or another developer makes a mistake and disconnects at the wrong time.

    Given that, I would just allow my connection to close automatically for me. Your pages should be executing quickly, therefore holding the connection for that small amount of time shouldn't cause any problems.

    0 讨论(0)
  • 2020-12-03 17:42

    The basic unit of execution presumably is an entire script. What you first of all are wanting to apply resources (i.e. the database) to, efficiently and effectively, is the entirety of a single script.

    However, PHP, Apache/IIS/whatever, have lives of their own; and they are capable of using the connections you open beyond the life of your script. That's the signficance of persistent (or pooled) connections.

    Back to your script. It turns out you have a great deal of opportunity to be creative about using that connection during its execution.

    The typical naive script will tend to hit the connection again and again, picking up locally appropriate scraps of data associated with given objects/modules/selected options. This is where procedural methodology can inflict a penalty on that connection by opening, requesting, receiving, and closing. (Note that any single query will remain alive until it is explicitly closed, or the script ends. Be careful to note that a connection and a query are not the same thing at all. Queries tie up tables; connections tie up ... connections (in most cases mapped to sockets). So you should be conscious of proper economy in the use of both.

    The most economical strategy with regard to queries is to have as few as possible. I'll often try to construct a more or less complex joined query that brings back a full set of data rather than parceling out the requests in small pieces.

    0 讨论(0)
  • 2020-12-03 17:47

    I just read this comment on PHP website regarding persistent connection and it might be interesting to know:

    Here's a recap of important reasons NOT to use persistent connections:

    • When you lock a table, normally it is unlocked when the connection closes, but since persistent connections do not close, any tables you accidentally leave locked will remain locked, and the only way to unlock them is to wait for the connection to timeout or kill the process. The same locking problem occurs with transactions. (See comments below on 23-Apr-2002 & 12-Jul-2003)

    • Normally temporary tables are dropped when the connection closes, but since persistent connections do not close, temporary tables aren't so temporary. If you do not explicitly drop temporary tables when you are done, that table will already exist for a new client reusing the same connection. The same problem occurs with setting session variables. (See comments below on 19-Nov-2004 & 07-Aug-2006)

    • If PHP and MySQL are on the same server or local network, the connection time may be negligible, in which case there is no advantage to persistent connections.

    • Apache does not work well with persistent connections. When it receives a request from a new client, instead of using one of the available children which already has a persistent connection open, it tends to spawn a new child, which must then open a new database connection. This causes excess processes which are just sleeping, wasting resources, and causing errors when you reach your maximum connections, plus it defeats any benefit of persistent connections. (See comments below on 03-Feb-2004, and the footnote at http://devzone.zend.com/node/view/id/686#fn1)

    (I was not the one that wrote the text above)

    0 讨论(0)
  • 2020-12-03 17:51

    Don't bother disconnecting. The cost of checking $_connected before each query combined with the cost of actually calling $db->disconnectFromDB(); to do the closing will end up being more expensive than just letting PHP close the connection when it is finished with each page.

    Reasoning:

    1: If you leave the connection open till the end of the script:

    • PHP engine loops through internal array of mysql connections
    • PHP engine calls mysql_close() internally for each connection

    2: If you close the connection yourself:

    • You have to check the value of $_connected for every single query. This means PHP has to check that the variable $_connected A) exists B) is a boolean and C) is true/false.
    • You have to call your 'disconnect' function, and function calls are one of the more expensive operations in PHP. PHP has to check that your function A) exists, B) is not private/protected and C) that you provided enough arguments to your function. It also has to create a copy of the $connection variable in the new local scope.
    • Then your 'disconnect' function will call mysql_close() which means PHP A) checks that mysql_close() exists and B) that you have provided all needed arguments to mysql_close() and C) that they are the correct type (mysql resource).

    I might not be 100% correct here but I believe the odds are in my favour.

    0 讨论(0)
提交回复
热议问题