[CONJ-39] When using the getGeneratedKeys() method, incorrent data is returned (depeneds on version) Created: 2013-05-22 Updated: 2013-05-28 Resolved: 2013-05-23 |
|
| Status: | Closed |
| Project: | MariaDB Connector/J |
| Component/s: | None |
| Affects Version/s: | 1.1.0, 1.1.1, 1.1.2 |
| Fix Version/s: | 1.1.3 |
| Type: | Bug | Priority: | Major |
| Reporter: | Amir Glaser | Assignee: | Vladislav Vaintroub |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Environment: |
Server: MySQL 5.0.26, running on SLES10 SP4 |
||
| Description |
|
The incorrect data is returned when executing a prepared statement (using the Statement.RETURN_GENERATED_KEYS flag) which inserts several new rows into a table with an auto Increment column. For version 1.1.0: For version 1.1.1/1.1.2: The expected result would be to receive a result set where the number of records is equal to the number of new keys generated, and that the values for these records will be the actual inserted keys. |
| Comments |
| Comment by Vladislav Vaintroub [ 2013-05-22 ] |
|
Could you attach a small but complete code example to illustrate your point? Single java class with main() is ok, but JUnit format is the best, since it will also illustrate what you're actually expecting with assertXXX. If you're talking about multivalued inserts, MySQL cannot return all autogenerated keys for all values. Because it is not sent with the protocol. |
| Comment by Amir Glaser [ 2013-05-22 ] |
|
I am not sure I will have time to create an example, but I'll try. The scenario can be very easily reproduced when using the MariaDB Java client to insert multiple rows into a table that has an AutoIncrement index column. When you say "multivalued" inserts - What do you mean? In my use-case, the table to which I am inserting only has one column which is defined as autoIncrement and it is also the primary key for the table. |
| Comment by Vladislav Vaintroub [ 2013-05-22 ] |
|
Maybe those are multi-row inserts, I forgot the correct name. single insert-multiple rows Like this Insert into t Because you're talking about insert that generated multiple autoincrement values, so I was wondering what kind of insert that is. Except the above, there is also insert-select insert into t select * from t2 however this , just like the previous example, won't return all autogenerated ids. So, I'm really intrigued by the test case you'll hopefully attach. Thanks! |
| Comment by Amir Glaser [ 2013-05-22 ] |
|
We recently moved from working the Connector/J to MariaDB. I know that when we were working with Connector/J, we were able to retrieve multiple generated IDs. |
| Comment by Vladislav Vaintroub [ 2013-05-22 ] |
|
Maybe. Anyway, to understand and to check what worked there and does not work here, I would need a code example, because I do not understand the problem. |
| Comment by Amir Glaser [ 2013-05-22 ] |
|
import java.sql.Connection; import org.apache.commons.dbcp.BasicDataSource; public class GeneratedKeys { public static void main(String[] args) { String query = "INSERT INTO DEMO (value) VALUES (?),(?),(?)"; List<String> queryParms = new ArrayList<String>(); queryParms.add("1"); queryParms.add("2"); queryParms.add("3"); GeneratedKeys k = new GeneratedKeys(); //Connect and execute with the connector: // List<Integer> keys = k.executeQuery("org.mariadb.jdbc.Driver", query, queryParms); List<Integer> keys = k.executeQuery("org.gjt.mm.mysql.Driver", query, queryParms); System.out.println(""); System.out.println("****** Results ********"); System.out.println("Generated Keys: "+keys.toString()); }public List<Integer> executeQuery(String driverName,String query, List<String> queryParms){ String serverName = "127.0.0.1"; BasicDataSource DataSource = new BasicDataSource(); //Connect to the DB //Execute the query } stmt.executeUpdate(); List<Integer> generatedKeys = new ArrayList<Integer>(); conn.close(); return generatedKeys; finally{ catch (Exception ee) { ee.printStackTrace(); }} return new ArrayList<Integer>(); |
| Comment by Amir Glaser [ 2013-05-22 ] |
|
Just added a source code example. In the example, I'm using the BasicDataSource class (from apache commons). The table named "DEMO" which I'm inserting in is defined using the following SQL: DROP TABLE IF EXISTS `DEMO`.`DEMO`; |
| Comment by Amir Glaser [ 2013-05-22 ] |
|
I tried to run this code having the mariaDB JAR in the classpath and using driver name: org.mariadb.jdbc.Driver - The resulting list contains one element (instead of 3) By the way, If I switch the MariaDB JAR in the classpath to the 1.1.0 version, the resulting list will have 3 elements, but the values are wrong. |
| Comment by Vladislav Vaintroub [ 2013-05-23 ] |
|
Indeed, ConnectorJ outsmarted the protocol. so we need to keep up |