473,569 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Maximum of 254 prepared statements per connection

>Description:
MySQL v4.1.0-alpha only allows a client to prepare a maximum of 254
statements. On the 255th mysql_prepare() call, a failure is returned
with no information returned by mysql_error(). This occurs even if
the statements are closed after each use.
How-To-Repeat:
The following program highlights the problem. Just change the connection
details to some database. It also assumes there is a table called dummy,
created with "CREATE TABLE dummy ( a VARCHAR(1024) )"

It was compiled using:
gcc -o buggy buggy.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lz
#include <mysql.h>
#include <stdio.h>
#include <string.h>

int queryCount = 0;

int
runQuery( MYSQL* mysql, const char* sql )
{
MYSQL_STMT* stmt = 0;

++queryCount;

// Prepare statement
stmt = mysql_prepare( mysql, sql, strlen( sql ) );
if ( !stmt )
{
fprintf( stderr, "Failed to prepare statement: %s\n", sql );
fprintf( stderr, "%s\n", mysql_error( mysql ) );
return 0;
}
printf( "Query number %d has statement id %u\n",
queryCount,
stmt->stmt_id );

// Execute statement
if ( mysql_execute( stmt ) != 0 )
{
fprintf( stderr, "Failed to execute prepared statement: %s\n", sql );
mysql_stmt_clos e( stmt );
return 0;
}

// Display number of affected rows
printf( "Query affected %d rows\n", mysql_stmt_affe cted_rows( stmt ) );
mysql_stmt_clos e( stmt );

return 1;
}

int
main( int argc, char* argv[] )
{
const char* sql = "INSERT INTO dummy VALUES ( 'Hello' )";
MYSQL* mysql = 0;
int i = 0;

my_init();

mysql = mysql_init( 0 );
if ( !mysql )
{
fprintf( stderr, "mysql_init failed\n" );
return( 1 );
}

// Connect to database
if ( !mysql_real_con nect( mysql,
"localhost" ,
"user",
"password",
"testDB",
0,
"/var/lib/mysql/mysql.sock",
0 ) )
{
fprintf( stderr, "Failed to connect to MySQL database\n" );
fprintf( stderr, "%s\n", mysql_error( mysql ) );
mysql_close( mysql );
return( 1 );
}
printf( "Connected to database\n" );

// Table dummy is just declared as:
//
// CREATE TABLE dummy ( a VARCHAR(1024) )
//
for ( i = 0; i < 3000; ++i )
{
if ( !runQuery( mysql, sql ) )
{
fprintf( stderr, "Failed to run query: %s\n", sql );
break;
}
}

// Disconnect from database
mysql_close( mysql );
printf( "Disconnect ed from database\n" );

return( 0 );
}
Fix:
After doing some investigation the problem seems to be with the MySQL
protocol. The server response to a mysql_prepare() is:

[STMT_ID:4]
[Column_count:2]
[Param_count:2]
[Columns meta info] (if Column_count > 0)
[Params meta info] (if Param_count > 0 ) (TODO : 4.1.1)

So, once the statement id reaches 255, the first byte in the response
sent by the server is 255(0xff). However in net_safe_read() in
libmysql/libmysql.c and libmysql_r/libmysql.c there is the following test:

if (net->read_pos[0] == 255)
{
<report an error>
}

which in this case is not true.

As a workaround, I changed mysql_stmt_prep are() in sql/sql_prepare.cc
using the following patch:

diff -r -u mysql-4.1.0-alpha.orig/sql/sql_prepare.cc mysql-4.1.0-alpha/sql/sql_prepare.cc
--- mysql-4.1.0-alpha.orig/sql/sql_prepare.cc 2003-04-03 22:15:51.000000 000 +1000
+++ mysql-4.1.0-alpha/sql/sql_prepare.cc 2003-07-21 15:51:40.000000 000 +1000
@@ -766,6 +766,17 @@
bzero((char*) &stmt, sizeof(stmt));

stmt.stmt_id= ++thd->current_stmt_i d;
+ /* Seems to be a bug in the MySQL protocol here. If last byte of
+ * stmt_id == 0xff then the client interprets this is an error.
+ * Refer to net_safe_read() in libmysql.c and notice how it checks
+ * if the first byte is 255. If so then it is an error.
+ * So just make sure that statement id cannot have 0xff as it's last
+ * byte.
+ */
+ while ( ( stmt.stmt_id & 0xff ) == 0xff )
+ {
+ stmt.stmt_id= ++thd->current_stmt_i d;
+ }
init_sql_alloc( &stmt.mem_ro ot, 8192, 8192);

stmt.thd= thd;
So just ensuring the statement id never has 0xff in the bottom byte.
Submitter-Id: <submitter ID>
Originator:
Organization : Safehouse International, Melbourne, AustraliaMySQL support: none
Synopsis: Maximum of 254 prepared statements per connection.
Severity: critical
Priority: medium
Category: mysql
Class: sw-bug
Release: mysql-4.1.0-alpha (Official MySQL RPM)
Server: /usr/bin/mysqladmin Ver 8.40 Distrib 4.1.0-alpha, for pc-linux on i686 Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version 4.1.0-alpha-Max
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 3 min 5 sec

Threads: 1 Questions: 9903 Slow queries: 0 Opens: 7 Flush tables: 1 Open tables: 1 Queries per second avg: 53.530C compiler: gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
C++ compiler: g++ (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
Environment:


System: Linux entrails.como 2.4.20-13.8.sh1smp #1 SMP Wed Jun 25 14:35:30 EST 2003 i686 i686 i386 GNU/Linux
Architecture: i686

Some paths: /usr/bin/perl /usr/bin/make /usr/bin/gcc /usr/bin/cc
GCC: Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --host=i386-redhat-linux --with-system-zlib --enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
Compilation info: CC='gcc' CFLAGS='-O2 -march=i686' CXX='g++' CXXFLAGS='-O2 -march=i686 -felide-constructors -fno-exceptions -fno-rtti ' LDFLAGS='' ASFLAGS=''
LIBC:
lrwxrwxrwx 1 root root 13 Jun 16 17:50 /lib/libc.so.6 -> libc-2.3.2.so
-rwxr-xr-x 1 root root 1292588 Apr 8 05:44 /lib/libc-2.3.2.so
-rw-r--r-- 1 root root 2323252 Apr 8 05:03 /usr/lib/libc.a
-rw-r--r-- 1 root root 204 Apr 8 04:54 /usr/lib/libc.so
Configure command: ./configure '--disable-shared' '--with-mysqld-ldflags=-all-static' '--with-client-ldflags=-all-static' '--without-berkeley-db' '--with-innodb' '--without-vio' '--without-openssl' '--enable-assembler' '--enable-local-infile' '--with-mysqld-user=mysql' '--with-unix-socket-path=/var/lib/mysql/mysql.sock' '--prefix=/' '--with-extra-charsets=comple x' '--exec-prefix=/usr' '--libexecdir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--localstatedir=/var/lib/mysql' '--infodir=/usr/share/info' '--includedir=/usr/include' '--mandir=/usr/share/man' '--with-embedded-server' '--enable-thread-safe-client' '--with-comment=Officia l MySQL RPM' 'CFLAGS=-O2 -march=i686' 'CXXFLAGS=-O2 -march=i686 -felide-constructors -fno-exceptions -fno-rtti '

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/my***********...ie.nctu.edu.tw

Jul 19 '05 #1
0 3002

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2261
by: Mark | last post by:
hello! mysqli in PHP5 comes with prepared statements functionality. However, without persistent connections or connection pooling in this code library, one has to ask: why bother? are prepared statements 'remembered' by the server for a while between connections, so that new connections can take advantage of them, or are they really...
3
21468
by: Deepali Gupta | last post by:
We are encountering the problem of "maximum open cursors exceeded" exception. Our web based system uses jdk1.3 as frontend with Oracle 9i as backend and Oracle JDBC Driver version - 9.0.2.0.0 for connectivity. Connection pooling is being used and the resultset is fetched in the functions and returned to JSPs where the functions are being...
1
3801
by: Tom D | last post by:
I'm rewriting a database interface that our company currently has. Currently it's using the Pear::DB interface, but we found that that was introducing a bit too much overhead. I'm rewriting the interface to use mysqli. Most of what the interface does is to simplify getting results in the form of arrays (ordered and associative). Most of the...
0
6232
by: Prashanth | last post by:
I am getting this error from BEA DB2 Driver in weblogic console. java.sql.SQLException: CURSOR C02 NOT IN A PREPARED STATE java.sql.SQLException: CURSOR C02 NOT IN A PREPARED STATE We keep running into this problem, frequently almost everyday. We figured what exactly to do when the problem happens but not the reason or the solution to...
2
28546
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
4
7793
by: NS | last post by:
Hi, I am trying to execute a prepare statement using oledb provider for DB2. The command.Prepare() statement is giving me an exception " No error information available: DB_E_NOCOMMAND(0x80040E0C)." My code is very simple and is working with other oledb provider, like SQL Server and oracle.
3
7310
by: birju | last post by:
Hi, I'm running SQL Profiler on an SQL Server 2000 database. I see that one stored procedure gets repeatedly executed having a handle of '1'. This query takes a long time to complete. How do I find what the text of the stored procedure is? I cant see any handle being created (using sp_prepare) with an id of '1' in the profiler. Is there any...
5
12245
by: alingsjtu | last post by:
Hello, every body. When execute dynamic generated multiple OPENQUERY statements (which linkes to DB2) in SQLServer, I always got SQL1040N The maximum number of applications is already connected to the database. SQLSTATE=57030. Background: I created a linked server to DB2 8.1 database which called GRR_DB2Server. In my stored procedure...
2
2724
by: ojorus | last post by:
Hi! Some questions regarding the mysqli-extension (php5) 1) Prepared statements: If I understand things right, prepared statements will give better performance if you make several similar querys. (where you only change the parameters) But what if you do only ONE query; will it then be usefull to use prepared statements? Can it actuelly give...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.