When extended INSERT (insert with several tuples in one query) is used with ON DUPLICATE KEY UPDATE clause and there're unique key conflict the Connector/J's Statement will return incorrect list of generated identifiers that cannot be matched with tuples from INSERT query at all. There are steps to reproduce this issue:
In fact MySql returns unexpected count of updated rows in such case because of this behavior:
- create a table with auto increment primary key and unique key:
create table TEST_TABLE( id int auto_increment, test_field varchar(255), primary key (id), unique key(test_field) ) engine=InnoDB;
- insert the following row:
insert into TEST_TABLE(id, test_field) values(100, 'test1');
- perform an extended insert with ON DUPLICATE KEY UPDATE clause via JDBC driver:
insert into TABLE(test_field) values('test2'),('test1'),('test3') on duplicate key update id=LAST_INSERT_ID(id);
- at first, you'll get 4 values instead of 3: [1, 2, 3, 4]
- at second, there's no correct id for test1 row in returned list
In fact MySql returns unexpected count of updated rows in such case because of this behavior:
For INSERT ... ON DUPLICATE KEY UPDATE statements, the affected-rows value is 1 if the row is inserted as a new row and 2 if an existing row is updated.I've reported new #61213 issue for MySQL Connector/J and according to Mark Matthews the fix raises a modification in MySQL server protocol because there's currently no way to get generated keys for whole data set though LAST_INSERT_ID returns only the first one. Thus be aware of this issue.
