我可以信任mysql_insert_id()返回正确的值,多线程妄想症 [英] can I trust mysql_insert_id() to return correct value, multithreading paranoia

查看:94
本文介绍了我可以信任mysql_insert_id()返回正确的值,多线程妄想症的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个在表中插入新行的函数,在对mysql调用INSERT语句后,我进行了一些检查,然后检查了mysql_insert_id()的值.

I made a function that inserts a new line into a table, after calling an INSERT statement into mysql, I then make some checks, and then I check the value of mysql_insert_id().

我想安静地知道在获得mysql_insert_id() 的结果之前,另一个线程不可能插入行插入.

I want to be quiet knowing that it is not possible for another thread to break in, inserting a row, before I get the result of mysql_insert_id().

我敢打赌,它应该很安全,但是我要问,因为这可能是灾难性的.

My bet is that it should be safe, but I am asking because this one can be disastrous.

推荐答案

来自

生成的ID保留在服务器上的 每个连接的基础.这意味着 给定客户端的功能是生成的第一个AUTO_INCREMENT值 对于通过该语句影响AUTO_INCREMENT列的最新语句 客户.该值不会受到其他客户端的影响,即使他们 生成自己的AUTO_INCREMENT值.此行为可确保 每个客户都可以检索自己的ID,而不必担心 其他客户的活动,不需要锁或 交易.

The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.

因此,如果您的线程将使用单独的连接,则它们将获得不同的插入ID.

So if your threads will use separate connections, they will get different insert IDs.

这篇关于我可以信任mysql_insert_id()返回正确的值,多线程妄想症的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆