本文介绍了事务中的LAST_INSERT_ID()是否可靠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mysql/ado.net/C#这是我的问题我知道mysql是并发的,但是我要同步文件数据(拇指名称)和数据库数据(行).

I am using mysql/ado.net/C# heres my problemI know mysql is concurrent, however i have file data (the thumbname name) and db data (the row) to be in sync.

如果我开始交易,但由于任何原因失败,这将是一个问题吗?如果我让这段代码同时在两个内核上运行,它们是否会互相干扰?我本质上需要知道是否1)last_insert_id依赖于不变. 2)如果某笔交易使用了该rowid,则另一笔交易将不使用它.

If i start a transaction and it fails for any reason will this be a problem? If i have this code ran at the same time on two cores will they clobber eachother? I essentially need to know if 1) the last_insert_id is dependable on not changing. 2) if one transaction uses said rowid that another transaction wont 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

这样安全吗?为什么或为什么不呢?

Is this safe? Why or why not?

推荐答案

MySQL的last_insert_id()是可靠的,因为它始终是该特殊连接执行的最后插入.它不会报告由其他连接创建的插入ID,也不会报告您在两个连接之前进行的插入.实际插入发生在哪个cpu内核上以及处理last_insert_id()调用在哪个内核上都没有关系.始终是该连接的正确ID号.

MySQL's last_insert_id() is dependable in that it's always the LAST insert performed by THAT PARTICULAR connection. It won't report an insert id created by some other connection, it won't report an insert that you did two connections ago. It won't matter which cpu core the actual insert occured on, and which core the last_insert_id() call is processed on. It will always be the right ID number for that connection.

如果您回滚执行插入操作的事务,则即使不再存在新的ID,last_insert_id()仍会报告该新ID.该ID将不会在以后的插入中重复使用

If you roll back a transaction that did an insert, last_insert_id() will STILL report that new id, even though it no longer exists. The id will not be reused, however, in a subsequent insert

这篇关于事务中的LAST_INSERT_ID()是否可靠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:42