You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MySqlConnector version: 1.3.11
Environment: .NET Framework 4.7.2 under Windows 10
MySqlCommand.LastInsertedId gives an incorrect result, when using multiple SQL-statements in the command.
This is the method that exposes the problem:
protected long NewEntryTransactional(string qry, params MySqlParameter[] parameters)
{
using MySqlConnection dbConnection = CreateDatabaseConnection();
using MySqlCommand cmd = CreateNewEntryCmd(dbConnection, qry, parameters); // Builds the cmd
dbConnection.Open();
try
{
MySqlTransaction tr = dbConnection.BeginTransaction();
try
{
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
long id = cmd.LastInsertedId;
tr.Commit();
return id;
}
catch (Exception)
{
tr.Rollback();
throw;
}
}
finally
{
dbConnection.Close();
}
}
When I call this method with qry containing a SELECT statement followed by an INSERT statement, then cmd.LastInsertedId returns -1 instead of the ID of the inserted row. I have also tried to moved long id = cmd.LastInsertedId; behind tr.Commit(); But this still returns -1. (The SELECT statement defines a variable that is used in the INSERT statement.)
Running the code with Connector/NET returns the ID. (EDIT: My original Connector/NET code did not include the line cmd.Transaction = tr;)
I looked a bit deeper into it: cmd.LastInsertedId does not work with a single INSERT statement outside of a transaction, either. This time it returns 0, even though a row with a new auto increment value had been inserted. (Checked with the InnoDB engine.)
cmd.LastInsertedId does not work with a single INSERT statement outside of a transaction, either.
I was able to narrow down the problem. A simple INSERT statement does indeed work. But when I add a comment sign (#) directly after the semicolon, then LastInsertedId returns 0.
The problem also shows up, when I surround the INSERT statement with a LOCK TABLE ... WRITE; before and an UNLOCK TABLE; instruction afterwards (in the same MySqlCommand object).
The problem also shows up, when I surround the INSERT statement with a LOCK TABLE ... WRITE; before and an UNLOCK TABLE; instruction afterwards (in the same MySqlCommand object).
I see 0 being returned from Connector/NET. OTOH, I could be convinced that's a bug (that MySqlConnector should fix); there's an INSERT command in that compound SQL statement, and LastInsertedId should return the first ID inserted from the last INSERT command (which would not be 0). What are your thoughts?
CREATE TABLE `test` ( `id` INT NOT NULL AUTO_INCREMENT , `foo` INT NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
LOCK TABLE test WRITE;
INSERT INTO test (foo) VALUES (42);
UNLOCK TABLE;
SELECT LAST_INSERT_ID();
This returns 1, and subsequent executions of LOCK TABLE ... UNLOCK TABLE; SELECT LAST_INSERT_ID(); return 2, 3, ...
Activity
dkoehler69 commentedon Aug 24, 2021
I looked a bit deeper into it:
cmd.LastInsertedIddoes not work with a single INSERT statement outside of a transaction, either. This time it returns 0, even though a row with a new auto increment value had been inserted. (Checked with the InnoDB engine.)Persist MySqlCommand.LastInsertedId. Fixes #1026
bgrainger commentedon Aug 24, 2021
Thanks for the detailed bug report; a fix is underway.
There are tests that verify that this does work:
MySqlConnector/tests/SideBySide/InsertTests.cs
Lines 33 to 37 in 20ddc50
If you have a consistent repro, please post it.
dkoehler69 commentedon Aug 24, 2021
I was able to narrow down the problem. A simple INSERT statement does indeed work. But when I add a comment sign (
#) directly after the semicolon, thenLastInsertedIdreturns 0.The problem also shows up, when I surround the INSERT statement with a
LOCK TABLE ... WRITE;before and anUNLOCK TABLE;instruction afterwards (in the sameMySqlCommandobject).Checked with MySqlConnector version: 1.3.11.
bgrainger commentedon Aug 24, 2021
I haven't tested yet, but those will likely be fixed by the commit already linked to this case.
bgrainger commentedon Aug 25, 2021
I see 0 being returned from Connector/NET. OTOH, I could be convinced that's a bug (that MySqlConnector should fix); there's an
INSERTcommand in that compound SQL statement, andLastInsertedIdshould return the first ID inserted from the lastINSERTcommand (which would not be 0). What are your thoughts?bgrainger commentedon Aug 25, 2021
Looks like Connector/NET's behaviour is already filed as a bug: https://bugs.mysql.com/bug.php?id=97061
dkoehler69 commentedon Aug 25, 2021
I checked what MySQL itself is returning:
This returns 1, and subsequent executions of
LOCK TABLE ... UNLOCK TABLE; SELECT LAST_INSERT_ID();return 2, 3, ...So yes, it seems to be a bug.
bgrainger commentedon Aug 25, 2021
Added some additional tests (for the above examples) in 76357a1.