Does LAST_INSERT_ID () depend on the transaction?

I am using mysql / ado.net / C # heres my problem I know that mysql is parallel, however I have file data (thumbname) and db data (string) for synchronization.

If I start a transaction and for some reason it doesn’t work, will this be a problem? If this code worked for me on two cores simultaneously, would they be clobber eachother? I need to know if 1) last_insert_id really does not change reliably. 2) if one transaction uses the specified rowid, that the other transaction does not use it.

start transaction insert statement, however i dont want it to be active yet; select LAST_INSERT_ID() File.Delete(lastid)//may exist from a transaction that failed File.Move(thumbImage, lastid) transaction.commit()//ok done 

It's safe? Why or why not?

+7
source share
1 answer

MySQL last_insert_id () is reliable in that it is always the LAST insert performed by this private connection. It will not report the insert identifier created by any other connection, it will not report the insert that you made two connections back. It doesn’t matter on which cpu core the actual insert is present, and on which core the last_insert_id () call is processed. It will always be the correct identification number for this connection.

If you roll back the transaction into which the insert is inserted, last_insert_id () will STILL report this new identifier, even if it no longer exists. The identifier will not be reused, however, in a subsequent insertion

+28
source

All Articles