Bug #27046 selectrow_array fails if mysql_server_prepare is enabled
Submitted: 12 Mar 2007 13:05 Modified: 21 Feb 2013 21:53
Reporter: Philip Stoev Email Updates:
Status: Unsupported Impact on me:
None 
Category:Connectors: DBD::mysql ( Perl ) Severity:S3 (Non-critical)
Version:4.003 OS:Linux (Fedora 6)
Assigned to: CPU Architecture:Any
Tags: Contribution

[12 Mar 2007 13:05] Philip Stoev
Description:
It appears that if selectrow_array() is used with mysql_server_prepare, then the prepared statement remains at the server and a repeated query will very soon reach the maximum number of prepared statements, causing the script to fail.

How to repeat:
use DBI;
my $dbh = DBI->connect('dbi:mysql:mysql_server_prepare=1');
while (1) {
        $dbh->selectrow_array("SELECT 1");
}

Will run for a while and then start failing with:

*DBD::mysql::db selectrow_array failed: Can't create more than max_prepared_stmt_count statements (current value: 16382)

Suggested fix:
Make sure selectrow_array and the related calls removes any prepared statements before completing. do() does not appear to be affected.
[12 Mar 2007 13:34] Philip Stoev
I can not see any mysql_stmt_close() in the DBD::mysql code. Maybe that is why the statement does not get freed?
[12 Mar 2007 14:40] Sveta Smirnova
Thank you for the report.

Verified as described.
[3 May 2007 11:01] Mika Raento
The following patch does fix the leak from a simple testcase and passes all test from 'make test'. Somebody with a better understanding of how the lifetime of DBD objects works should review it. This is against 4.004.

<pre>
Index: dbdimp.c
===================================================================
--- dbdimp.c    (revision 5029)
+++ dbdimp.c    (revision 5030)
@@ -3783,6 +3783,8 @@
     mysql_st_free_result_sets(sth, imp_sth);
   }
   DBIc_ACTIVE_off(imp_sth);
+  if (imp_sth->stmt) mysql_stmt_close(imp_sth->stmt);
+  imp_sth->stmt=0;
   if (dbis->debug >= 2)
   {
     PerlIO_printf(DBILOGFP, "\n<-- dbd_st_finish\n");
</pre>
[13 Jul 2009 15:14] liz drachnik
Hello Mika
 In order for us to continue the process of reviewing your contribution
 to MySQL - We need you to review and sign the Sun|MySQL contributor
 agreement (the "SCA")

 The process is explained here:
 http://forge.mysql.com/wiki/Sun_Contributor_Agreement

 Getting a signed/approved SCA on file will help us facilitate your
 contribution-- this one, and others in the future.

 Thank you !

 Liz Drachnik  - Program Manager - MySQL
[2 Oct 2009 23:00] Bugs System
No feedback was provided for this bug for over a month, 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".
[4 Nov 2009 0:00] Bugs System
No feedback was provided for this bug for over a month, 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".
[21 Feb 2013 21:53] Sveta Smirnova
Thank you for the report.

We don't work on DBD::mysql bugs anymore. All its bugs should go to CPAN: https://rt.cpan.org/Public/Dist/Display.html?Name=DBD-mysql

I re-submitted your report to https://rt.cpan.org/Public/Bug/Display.html?id=83516
Please subscribe to the new report on CPAN and work with DBD::mysql developers in case if they need additional details.