#32866 [Bgs]: implicit (and redundant) mysql_close on mysql resource destruction

2005-04-28 Thread kevin at hatry dot com
 ID:   32866
 User updated by:  kevin at hatry dot com
 Reported By:  kevin at hatry dot com
 Status:   Bogus
 Bug Type: MySQL related
 Operating System: windows XP + linux
 PHP Version:  4.3.11
 New Comment:

i didn't think i was having a discussion just thought you hadn't seen
my point and wanted to make it clear.
But if you say that it is normal that "affecting "NULL" to $link1 makes
$link2 to become "Unkown"" i will not bother you anymore.

I understand that you are all very busy (as i am by the way, this
problem has put me way behind schedule) and i am sorry that my feedback
has made you lose your time.


Previous Comments:


[2005-04-28 14:13:35] [EMAIL PROTECTED]

Please don't abuse the bug system for questions and discussions. Use
PHP's mailinglists (http://www.php.net/mailing-lists.php)



[2005-04-28 13:48:26] kevin at hatry dot com

thank you for your direct answer however this "solution" cannot work
with a heavily loaded website as it would open too much connections on
the mysql server :
for example a page called 50 times a second and opening 3 connections
at a the same time means 150 connections /sec to the mysql server
instead of 50 ! 
This situation is clearly one where the shared connection is useful.

i will try one last example to make sure you really know how i "want it
to work", please read it through and answer the last question below,
thank you.

the following script (without any function to make it clearer) :



generate this output :

link1 : resource(4) of type (mysql link)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after closing link1 :
link1 : resource(4) of type (mysql link)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after link1 = NULL :
link1 : NULL
link2 : resource(4) of type (Unknown)
Warning: mysql_thread_id(): 4 is not a valid MySQL-Link resource in ...
on line 24
mysql thread : 
Warning: mysql_query(): 4 is not a valid MySQL-Link resource in ... on
line 28


If you could please answer this one last question :

why does affecting "NULL" to $link1 make $link2 to become "Unkown"  ?
In other words, why don't we see this output instead :

link1 : resource(4) of type (mysql link)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after closing link1 :
link1 : resource(4) of type (Unknown)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after link1 = NULL :
link1 : NULL
link2 : resource(4) of type (mysql link)
mysql thread : 16


If this is a feature than it should be documented as it is not obvious
that changing the value of the resource variable will close the
connection, even if it has been closed before.



[2005-04-28 12:49:42] [EMAIL PROTECTED]

Change false to true in mysql_connect() and it will work like  you want
it to work. Still not a bug.




[2005-04-28 12:26:33] kevin at hatry dot com

Description:

I originally posted this info on follow up of bug id 30525 but it has
been reported as bogus and i dont think the problem has been fully
understood.
Please *do* try to understand the problem here as there *is* one !

PHP version used :
-> php 4.3.11 on win32 with the bundled 3.23.49 mysql client
-> php 4.3.11 on linux kernel 2.4.29 with external 4.1.11 mysql client

both with apache 1.3.33 and with the cli binary.

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 but that there now appears to be reference counting
before finally closing the TCP connection which is a better thing for
me.

The problem is :
It seems that affecting a value or 'unset'ing a variable of type mysql
resource or living the function in which the variable was created calls
mysql_close on that variable (and so reduces the reference) even if a
previous call was already made on that variable. So the reference count
becomes wrong.

So it is very hard to keep track of the real status (connected, not
connected, of type mysql, of type unknown) of a mysql resource.

The reproduce code is quite simple but is, i think, a typical way of
programming : you have a function (or class) that query mysql by
creating its own resource.
Normally, with this version of PHP, the call to mysql_close should not
close the shared connection so the code is ok to use. But with the bug
a second call to mysl_close is made when the function returns.

A last note : the same bug can be found if, instead of calling the
function "do_something_in_mysql" we do $link1 = -1; after the line
"mysql_close($link1);".

Thank you for your time, i hope you will see the bug this time.

Reproduce code:
---
 the bug.

echo "link1

#32866 [Bgs]: implicit (and redundant) mysql_close on mysql resource destruction

2005-04-28 Thread georg
 ID:   32866
 Updated by:   [EMAIL PROTECTED]
 Reported By:  kevin at hatry dot com
 Status:   Bogus
 Bug Type: MySQL related
 Operating System: windows XP + linux
 PHP Version:  4.3.11
 New Comment:

Please don't abuse the bug system for questions and discussions. Use
PHP's mailinglists (http://www.php.net/mailing-lists.php)


Previous Comments:


[2005-04-28 13:48:26] kevin at hatry dot com

thank you for your direct answer however this "solution" cannot work
with a heavily loaded website as it would open too much connections on
the mysql server :
for example a page called 50 times a second and opening 3 connections
at a the same time means 150 connections /sec to the mysql server
instead of 50 ! 
This situation is clearly one where the shared connection is useful.

i will try one last example to make sure you really know how i "want it
to work", please read it through and answer the last question below,
thank you.

the following script (without any function to make it clearer) :



generate this output :

link1 : resource(4) of type (mysql link)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after closing link1 :
link1 : resource(4) of type (mysql link)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after link1 = NULL :
link1 : NULL
link2 : resource(4) of type (Unknown)
Warning: mysql_thread_id(): 4 is not a valid MySQL-Link resource in ...
on line 24
mysql thread : 
Warning: mysql_query(): 4 is not a valid MySQL-Link resource in ... on
line 28


If you could please answer this one last question :

why does affecting "NULL" to $link1 make $link2 to become "Unkown"  ?
In other words, why don't we see this output instead :

link1 : resource(4) of type (mysql link)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after closing link1 :
link1 : resource(4) of type (Unknown)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after link1 = NULL :
link1 : NULL
link2 : resource(4) of type (mysql link)
mysql thread : 16


If this is a feature than it should be documented as it is not obvious
that changing the value of the resource variable will close the
connection, even if it has been closed before.



[2005-04-28 12:49:42] [EMAIL PROTECTED]

Change false to true in mysql_connect() and it will work like  you want
it to work. Still not a bug.




[2005-04-28 12:26:33] kevin at hatry dot com

Description:

I originally posted this info on follow up of bug id 30525 but it has
been reported as bogus and i dont think the problem has been fully
understood.
Please *do* try to understand the problem here as there *is* one !

PHP version used :
-> php 4.3.11 on win32 with the bundled 3.23.49 mysql client
-> php 4.3.11 on linux kernel 2.4.29 with external 4.1.11 mysql client

both with apache 1.3.33 and with the cli binary.

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 but that there now appears to be reference counting
before finally closing the TCP connection which is a better thing for
me.

The problem is :
It seems that affecting a value or 'unset'ing a variable of type mysql
resource or living the function in which the variable was created calls
mysql_close on that variable (and so reduces the reference) even if a
previous call was already made on that variable. So the reference count
becomes wrong.

So it is very hard to keep track of the real status (connected, not
connected, of type mysql, of type unknown) of a mysql resource.

The reproduce code is quite simple but is, i think, a typical way of
programming : you have a function (or class) that query mysql by
creating its own resource.
Normally, with this version of PHP, the call to mysql_close should not
close the shared connection so the code is ok to use. But with the bug
a second call to mysl_close is made when the function returns.

A last note : the same bug can be found if, instead of calling the
function "do_something_in_mysql" we do $link1 = -1; after the line
"mysql_close($link1);".

Thank you for your time, i hope you will see the bug this time.

Reproduce code:
---
 the bug.

echo "link1 : "; var_dump($link1);
echo "link2 : "; var_dump($link2);

function do_something_in_mysql () {
 $link3 = mysql_connect('localhost','root','',false);
 echo "link3 : "; var_dump($link3);
 // do some common queries here
 mysql_close($link3);
}
?>


Expected result:

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 (mysql link)


because $link2 has not been explicitly clo

#32866 [Bgs]: implicit (and redundant) mysql_close on mysql resource destruction

2005-04-28 Thread kevin at hatry dot com
 ID:   32866
 User updated by:  kevin at hatry dot com
 Reported By:  kevin at hatry dot com
 Status:   Bogus
 Bug Type: MySQL related
 Operating System: windows XP + linux
 PHP Version:  4.3.11
 New Comment:

thank you for your direct answer however this "solution" cannot work
with a heavily loaded website as it would open too much connections on
the mysql server :
for example a page called 50 times a second and opening 3 connections
at a the same time means 150 connections /sec to the mysql server
instead of 50 ! 
This situation is clearly one where the shared connection is useful.

i will try one last example to make sure you really know how i "want it
to work", please read it through and answer the last question below,
thank you.

the following script (without any function to make it clearer) :



generate this output :

link1 : resource(4) of type (mysql link)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after closing link1 :
link1 : resource(4) of type (mysql link)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after link1 = NULL :
link1 : NULL
link2 : resource(4) of type (Unknown)
Warning: mysql_thread_id(): 4 is not a valid MySQL-Link resource in ...
on line 24
mysql thread : 
Warning: mysql_query(): 4 is not a valid MySQL-Link resource in ... on
line 28


If you could please answer this one last question :

why does affecting "NULL" to $link1 make $link2 to become "Unkown"  ?
In other words, why don't we see this output instead :

link1 : resource(4) of type (mysql link)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after closing link1 :
link1 : resource(4) of type (Unknown)
link2 : resource(4) of type (mysql link)
mysql thread : 16
-- after link1 = NULL :
link1 : NULL
link2 : resource(4) of type (mysql link)
mysql thread : 16


If this is a feature than it should be documented as it is not obvious
that changing the value of the resource variable will close the
connection, even if it has been closed before.

-- 
Edit this bug report at http://bugs.php.net/?id=32866&edit=1