Bug #53311 The 6.0 client always return mysql_stmt_errno==0 after a reconnection occurred
Submitted: 30 Apr 2010 10:23 Modified: 10 Feb 2011 15:52
Reporter: YY Bear Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / C Severity:S2 (Serious)
Version:5.1, 5.6.99, 6.0.2 OS:Any (FreeBSD, MacOSX)
Assigned to: CPU Architecture:Any

[30 Apr 2010 10:23] YY Bear
Description:
The bug mentioned in Bug #43560 isn't fixed well
Although crash is fixed, mysql_stmt_errno() always return 0 after  a recconect occurred followed by mysql_stmt_execute()

How to repeat:
I compiled the attached 34651.c in Bug #43560 with MYSQL_OPT_RECONNECT enabled, and linked with libmysqlclient.a of various version on FreeBSD 6.3-RELEASE, there are the results: 
(Server version: 5.1.24-rc-log MySQL Community Server (GPL))
(I set wait_timeout to 10 sec by typying set global wait_timeout=10; in mysql client)

==========
linked wiht libmysqlclient.a in mysql-5.0.22-freebsd6.0-i386:
OK: mysql= mysql_init(NULL)
OK: mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect)
OK: mysql_real_connect(mysql, "127.0.0.1", NULL, NULL, "test", 0, NULL, 0)
OK: mysql_query(mysql, DROP)
OK: mysql_query(mysql, CREATE)
OK: stmt= mysql_stmt_init(mysql)
OK: mysql_stmt_prepare(stmt, INSERT, strlen(INSERT))
OK: mysql_stmt_bind_param(stmt, &bind)
OK: mysql_stmt_execute(stmt)
OK: mysql_stmt_execute(stmt)
please restart MySQL server and press <return>

(HERE, I JUST WAIT 10 SEC FOR TIMEOUT)

Error: 0 != mysql_stmt_execute(stmt)
MySQL error: 1243 - Unknown prepared statement handler (1) given to mysql_stmt_execute
Error: 0 != mysql_stmt_execute(stmt)
MySQL error: 2013 - Lost connection to MySQL server during query
OK: mysql_stmt_close(stmt)
OK: mysql_query(mysql, DROP)
complete

==========
linked wiht libmysqlclient.a in mysql-5.0.89-freebsd6.3-i386:
OK: mysql= mysql_init(NULL)
OK: mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect)
OK: mysql_real_connect(mysql, "127.0.0.1", NULL, NULL, "test", 0, NULL, 0)
OK: mysql_query(mysql, DROP)
OK: mysql_query(mysql, CREATE)
OK: stmt= mysql_stmt_init(mysql)
OK: mysql_stmt_prepare(stmt, INSERT, strlen(INSERT))
OK: mysql_stmt_bind_param(stmt, &bind)
OK: mysql_stmt_execute(stmt)
OK: mysql_stmt_execute(stmt)
please restart MySQL server and press <return>

(HERE, I JUST WAIT 10 SEC FOR TIMEOUT)

Error: 0 != mysql_stmt_execute(stmt)
MySQL error: 0 - Lost connection to MySQL server during query
Error: 0 != mysql_stmt_execute(stmt)
MySQL error: 2013 - Lost connection to MySQL server during query
OK: mysql_stmt_close(stmt)
OK: mysql_query(mysql, DROP)
complete

==========
linked wiht libmysqlclient.a in mysql-5.1.45-freebsd6.3-i386:
OK: mysql= mysql_init(NULL)
OK: mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect)
OK: mysql_real_connect(mysql, "127.0.0.1", NULL, NULL, "test", 0, NULL, 0)
OK: mysql_query(mysql, DROP)
OK: mysql_query(mysql, CREATE)
OK: stmt= mysql_stmt_init(mysql)
OK: mysql_stmt_prepare(stmt, INSERT, strlen(INSERT))
OK: mysql_stmt_bind_param(stmt, &bind)
OK: mysql_stmt_execute(stmt)
OK: mysql_stmt_execute(stmt)
please restart MySQL server and press <return>

(HERE, I JUST WAIT 10 SEC FOR TIMEOUT)

Error: 0 != mysql_stmt_execute(stmt)
MySQL error: 0 - Lost connection to MySQL server during query
Error: 0 != mysql_stmt_execute(stmt)
MySQL error: 0 - Lost connection to MySQL server during query
OK: mysql_stmt_close(stmt)
OK: mysql_query(mysql, DROP)
complete

==========
linked wiht libmysqlclient.a in mysql-connector-c-6.0.2-freebsd6-x86-32bit:
OK: mysql= mysql_init(NULL)
OK: mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect)
OK: mysql_real_connect(mysql, "127.0.0.1", NULL, NULL, "test", 0, NULL, 0)
OK: mysql_query(mysql, DROP)
OK: mysql_query(mysql, CREATE)
OK: stmt= mysql_stmt_init(mysql)
OK: mysql_stmt_prepare(stmt, INSERT, strlen(INSERT))
OK: mysql_stmt_bind_param(stmt, &bind)
OK: mysql_stmt_execute(stmt)
OK: mysql_stmt_execute(stmt)
please restart MySQL server and press <return>

(HERE, I JUST WAIT 10 SEC FOR TIMEOUT)

Error: 0 != mysql_stmt_execute(stmt)
MySQL error: 0 - Lost connection to MySQL server during query
Error: 0 != mysql_stmt_execute(stmt)
MySQL error: 0 - Lost connection to MySQL server during query
OK: mysql_stmt_close(stmt)
OK: mysql_query(mysql, DROP)
complete

==========

We can see that when there is a recconect occurred followed by mysql_stmt_execute(), the
mysql_stmt_execute() fails, 
and mysql_stmt_errno() returns 1243(ER_UNKNOWN_STMT_HANDLER), when linked with
libmysqlclient.a in mysql-5.0.22-freebsd6.0-i386,
but it returns 0(which means no error occurred?), when linked with libmysqlclient.a in
mysql-5.0.89-freebsd6.3-i386, mysql-5.1.45-freebsd6.3-i386 and mysql-connector-c-6.0.2-freebsd6-x86-32bit

In my application, I use the mysql_stmt_errno() to determine whether to reset all prepared statement function:

	if(mysql_stmt_execute(m_stmt))
	{
		int err = mysql_stmt_errno(m_stmt);
 		if (err == CR_SERVER_LOST || err == ER_UNKNOWN_STMT_HANDLER)
		{
			// Rebuild prepared statement function
			...
		}
		else
		{
			// Unknown error
			Assert(0);
		}
	}

If worked fine with libmysqlclient.a in mysql-5.0.22-freebsd6.0-i386, but after I updating to mysql-connector-c-6.0.2-freebsd6-x86-32bit it asserted for
mysql_stmt_execute() returning an ZERO

Actually, in my application I find that mysql_stmt_errno() may returns: 0,
2013(CR_SERVER_LOST), 1210(ER_WRONG_ARGUMENTS) and also 1243(ER_UNKNOWN_STMT_HANDLER)
[12 May 2010 1:28] YY Bear
I found that if there were many prepared statement functions, once a reconnection occurred, only a part of them would result in failure on calling mysql_stmt_execute(), and the others would not. What is more strange, some of these stmt functions can be successfully called by mysql_stmt_execute(), but actually the mysql server does complete different queries with disordered parameters.
[12 May 2010 1:41] YY Bear
Change S1 -> S2
[27 May 2010 18:42] Sveta Smirnova
Thank you for the report.

Verified as described. C API also affected.
[31 Jan 2011 17:46] Georg Richter
fixed in 6.0.3
[10 Feb 2011 15:52] Tony Bedford
An entry has been added to the 6.0.3 changelog:

mysql_stmt_errno() always returned 0 after a reconnect occurred followed by mysql_stmt_execute().