Bug #15249 | Prepared select statement with "<primaryKey> IS NULL" delivers wrong result | ||
---|---|---|---|
Submitted: | 25 Nov 2005 17:04 | Modified: | 28 Nov 2005 19:15 |
Reporter: | Markus Lehmann | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | Connector / J | Severity: | S1 (Critical) |
Version: | 4.13, 4.15 JDBC Connector 3.1.11 | OS: | MacOS (Mac OS X) |
Assigned to: | Vasily Kishkin | CPU Architecture: | Any |
[25 Nov 2005 17:04]
Markus Lehmann
[26 Nov 2005 8:27]
Vasily Kishkin
Could you please write here table definition ?
[26 Nov 2005 14:39]
Markus Lehmann
Here is the table definition (actually I do a select on all columns (explicitly named)): CREATE TABLE `department` ( `SEQ_NUM` bigint(20) NOT NULL auto_increment, `CREATION` datetime NOT NULL default '0000-00-00 00:00:00', `CR_USER_LINK` bigint(20) default NULL, `MODIFICATION` datetime NOT NULL default '0000-00-00 00:00:00', `MD_USER_LINK` bigint(20) default NULL, `DEPT_TYPE` char(1) NOT NULL default '', `NAME` varchar(64) NOT NULL default '', `ACCOUNT` varchar(10) default NULL, PRIMARY KEY (`SEQ_NUM`), KEY `NAME_IDX` (`NAME`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
[28 Nov 2005 1:42]
Mark Matthews
Does the query work if you add "useServerPrepStmts=false" as a JDBC configuration parameter? (if so, the bug is most likely with the server's prepared statement feature).
[28 Nov 2005 13:40]
Markus Lehmann
Setting useServerPrepStmts=false in the URL does not change the behavior. I tried to isolate the bug and I am not sure that the problem is in the JDBC driver because the scenario is this: 1. insert department (column SEQ_NUM is primary key and value is auto generated) 2. commit 3. select SEQ_NUM,... WHERE FROM DEPARTMENT WHERE SEQ_NUM IS NULL (in a prepared statement, same database connection) Returns the newly created department (may be there is a problem with a cache that has not been modifiesd to contain the auto generated value for SEQ_NUM?).
[28 Nov 2005 14:40]
Mark Matthews
If you're trying to make IS NULL return the record with the most recently auto-generated key, that only works for ODBC. Other clients do not set the flag on the server that causes this behavior because it is not standard. You either need to use LAST_INSERT_ID() in the query, or use the JDBC method getGeneratedKeys() on your Statement to retrieve the value and set it as a parameter in your query.
[28 Nov 2005 17:07]
Markus Lehmann
Well, the sql described in my last comment is some kind of auto generated (from a database framework), so it is hard to see the reason behind. Actually I do not expect to get records back from the select, the problem is that I actually get the last record back...
[28 Nov 2005 18:10]
Mark Matthews
Sorry, had it backwards, this behavior is default with the server, see: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html (look for "IS NULL"). You can disable this behavior by adding "sessionVariables=SQL_AUTO_IS_NULL=0" as part of your JDBC connection configuration properties.
[28 Nov 2005 19:15]
Markus Lehmann
I use now a SET SESSION QL_AUTO_IS_NULL=0 and it works. However I think that people using JDBC are not expecting the default behavior - so what about having the JDBC driver to send this command are creating a connection / session automatically
[28 Nov 2005 19:31]
Mark Matthews
I really can't comment on whether a user expects the behavior or not just because it's JDBC. It's actually non-sensical to issue the query, given that with the feature turned off, you should almost always get no records returned, since most often users utilize AUTO_INCREMENT for primary keys, so the column in question will never be NULL. There's an extra round-trip between client and server to set this by default (which most users do _not_ want), and it can be configured on the server as well via the "init_file" configuration parameter which will run the "SET ..." on the server.