#30525 [Opn->Bgs]: mysql_close() fails in some situations with shared links
ID: 30525 Updated by: [EMAIL PROTECTED] Reported By: l dot cameron2 at ugrad dot unimelb dot edu dot au -Status: Open +Status: Bogus Bug Type: MySQL related Operating System: * PHP Version: 5.0.x and 4.x New Comment: Please stop reopening this, this is not a bug but just how it works. Previous Comments: [2005-04-29 01:56:54] l dot cameron2 at ugrad dot unimelb dot edu dot au Please reread the first post where I explicitly talked about shared connection behaviour. The bug is that when using shared connections, if you ever use mysql_close() *and* set your links to null, your shared connections will be closed prematurely. As outlined above, the net result of the is that you can never call mysql_close() on a shared connection and expect to have it work correctly. [2005-04-27 14:48:13] [EMAIL PROTECTED] Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php When using same connection parameters no new connection will be established by default, instead an exisiting one will be reused. Set the optional newlink parameter to true if you want to establish more than one physical connection. [2005-04-27 13:25:23] kevin at hatry dot com i just want to add a last comment to make clear that this bug is important. this is (i think) a much used type of program : fails with : link1:resource(4) of type (mysql link) link3:resource(4) of type (mysql link) Warning: mysql_close(): 4 is not a valid MySQL-Link resource in ... on line 14 link1:resource(4) of type (Unknown) i think that closing a connection in the same environnement that it was open (like in the function here) is good programming so it shouldn't fail like that ! And the problem is *not* that the mysql_close closed all connections as shown by this example : here there is no failure : link1:resource(4) of type (mysql link) link2:resource(4) of type (mysql link) link3:resource(4) of type (mysql link) link1:resource(4) of type (Unknown) link2:resource(4) of type (Unknown) there would be one if we had closed $link2 after $link1 for example. [2004-10-22 06:19:48] l dot cameron2 at ugrad dot unimelb dot edu dot au Description: Preface: I *do* understand that by default MySQL connections are shared in PHP. I also note that in older versions a single mysql_close() would close all of the links; see #9107: there now there appears to be reference counting before finally closing the TCP connection -- which is IMHO better than the older behaviour, but the implementation has its own bugs: PHP appears to keeps an internal count of the number of times a link has been duplicated. When the link count is <= 0, the underlying TCP connection is actually closed. * close() reduces the link count by 1 * setting the connection to null *also* reduces the link count by 1 -- even if that link has already been close()d Currently the only workarounds for this are to: [1] Set new_link to true every time you mysql_connect() -- potentially creating a lot of TCP connections and slowing the program down [2] close() the link, but never set it to null and hope that PHP won't clean it up until the end of the program: This *will* fail sometimes though; see the example at http://www.levi.id.au/mysql4.php.txt [3] Never mysql_close() links, only set them to null and hope that PHP will in fact clean up the TCP connection before MySQL runs out of available connections (admittedly only a problem when you have a lot of simultaneous connections to your database) -- this does work now, but we're not supposed to assume anything about when PHP does its object destruction. The third is really the only viable solution; but is dependent on the internal implementation of the MySQL extension. At best, the current situation is that if you ever have shared links, you should never call mysql_close if you ever expect to use that database again in your program. Reproduce code: --- Simple example: #!/usr/local/bin/php -q See also the example at http://www.levi.id.au/mysql4.php.txt Expected result: Blank output. Actual result: -- PHP Warning: mysql_close(): 1 is not a valid MySQL-Link resource in /home/levi/public_html/mysql2.php on line 10 Warning: mysql_close(): 1 is not a valid MySQL-Link resource in /home/levi/public_html/mysql2.php on line 10 (If I remove the mysql_close($conn1); it works) (If I remove the $conn1 = null; it also works) -
#30525 [Opn->Bgs]: mysql_close() fails in some situations with shared links
ID: 30525 Updated by: [EMAIL PROTECTED] Reported By: l dot cameron2 at ugrad dot unimelb dot edu dot au -Status: Open +Status: Bogus Bug Type: MySQL related Operating System: win32 PHP Version: 5.0.x and 4.x New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php When using same connection parameters no new connection will be established by default, instead an exisiting one will be reused. Set the optional newlink parameter to true if you want to establish more than one physical connection. Previous Comments: [2005-04-27 13:25:23] kevin at hatry dot com i just want to add a last comment to make clear that this bug is important. this is (i think) a much used type of program : fails with : link1:resource(4) of type (mysql link) link3:resource(4) of type (mysql link) Warning: mysql_close(): 4 is not a valid MySQL-Link resource in ... on line 14 link1:resource(4) of type (Unknown) i think that closing a connection in the same environnement that it was open (like in the function here) is good programming so it shouldn't fail like that ! And the problem is *not* that the mysql_close closed all connections as shown by this example : here there is no failure : link1:resource(4) of type (mysql link) link2:resource(4) of type (mysql link) link3:resource(4) of type (mysql link) link1:resource(4) of type (Unknown) link2:resource(4) of type (Unknown) there would be one if we had closed $link2 after $link1 for example. [2005-04-27 03:24:29] l dot cameron2 at ugrad dot unimelb dot edu dot au Changing status back to open; The bug still exists in 5.0CVS as of 2005-04-27 (The sample code I gave still fails). Note kevin at hatry dot com's comments that it also exists in 4.x [2005-04-26 18:00:49] kevin at hatry dot com this bug also exists with php 4.3.11 on both win32 (mysql client 3.23.49) and linux (mysql client 4.1.9). Reproduce code: this outputs : link1:resource(4) of type (mysql link) link2:resource(4) of type (mysql link) link1:resource(4) of type (mysql link) link2:resource(4) of type (mysql link) and both links are usable for mysql_query even after the mysql_close on one of them. Is is a feature or a bug ? (would be ok as a feature i think) If you uncomment line 13 ($link2 = -1;) we have : link1:resource(4) of type (mysql link) link2:resource(4) of type (mysql link) link1:resource(4) of type (Unknown) link2:int(-1) $link1 is no longer a mysql link resource even though we only closed $link2 ! In fact if you comment the mysql_close but keep the line 13 uncommented, the output is : link1:resource(4) of type (mysql link) link2:resource(4) of type (mysql link) link1:resource(4) of type (mysql link) link2:int(-1) and there $link1 is usable. So it seems that affecting a value (-1, NULL or 'unset'ing) to a mysql resource do an implicit mysql_close but if that resource was already closed we have the equivalent of 2 calls to mysql_close and that is the bug. [2005-03-15 01:00:23] php-bugs at lists dot php dot net No feedback was provided for this bug for over a week, so it is being suspended automatically. If you are able to provide the information that was originally requested, please do so and change the status of the bug back to "Open". [2005-03-07 22:31:13] [EMAIL PROTECTED] Please try using this CVS snapshot: http://snaps.php.net/php5-latest.tar.gz For Windows: http://snaps.php.net/win32/php5-win32-latest.zip ..and update the version field if this still happens, otherwise close this report. The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/30525 -- Edit this bug report at http://bugs.php.net/?id=30525&edit=1
#30525 [Opn->Bgs]: mysql_close() fails in some situations with shared links
ID: 30525 Updated by: [EMAIL PROTECTED] Reported By: l dot cameron2 at ugrad dot unimelb dot edu dot au -Status: Open +Status: Bogus Bug Type: MySQL related Operating System: Fedora Core 2 PHP Version: 5.0.2 New Comment: Submitted twice (see #30526). Previous Comments: [2004-10-22 06:19:48] l dot cameron2 at ugrad dot unimelb dot edu dot au Description: Preface: I *do* understand that by default MySQL connections are shared in PHP. I also note that in older versions a single mysql_close() would close all of the links; see #9107: there now there appears to be reference counting before finally closing the TCP connection -- which is IMHO better than the older behaviour, but the implementation has its own bugs: PHP appears to keeps an internal count of the number of times a link has been duplicated. When the link count is <= 0, the underlying TCP connection is actually closed. * close() reduces the link count by 1 * setting the connection to null *also* reduces the link count by 1 -- even if that link has already been close()d Currently the only workarounds for this are to: [1] Set new_link to true every time you mysql_connect() -- potentially creating a lot of TCP connections and slowing the program down [2] close() the link, but never set it to null and hope that PHP won't clean it up until the end of the program: This *will* fail sometimes though; see the example at http://www.levi.id.au/mysql4.php.txt [3] Never mysql_close() links, only set them to null and hope that PHP will in fact clean up the TCP connection before MySQL runs out of available connections (admittedly only a problem when you have a lot of simultaneous connections to your database) -- this does work now, but we're not supposed to assume anything about when PHP does its object destruction. The third is really the only viable solution; but is dependent on the internal implementation of the MySQL extension. At best, the current situation is that if you ever have shared links, you should never call mysql_close if you ever expect to use that database again in your program. Reproduce code: --- Simple example: #!/usr/local/bin/php -q See also the example at http://www.levi.id.au/mysql4.php.txt Expected result: Blank output. Actual result: -- PHP Warning: mysql_close(): 1 is not a valid MySQL-Link resource in /home/levi/public_html/mysql2.php on line 10 Warning: mysql_close(): 1 is not a valid MySQL-Link resource in /home/levi/public_html/mysql2.php on line 10 (If I remove the mysql_close($conn1); it works) (If I remove the $conn1 = null; it also works) -- Edit this bug report at http://bugs.php.net/?id=30525&edit=1