已知:
在数据库表自增的情况下,mybatis执行完一条insert sql后,Jdbc3KeyGenerator获取主键的逻辑,是在insert语句后获取最大的id。
场景:
线程A:插入insert语句先执行,然后执行Jdbc3KeyGenerator获取最大的id。
那么在线程A的Jdbc3KeyGenerator获取最大id前一时刻,线程B:又有insert语句执行了。
问:
那线程A获取到的id是线程B执行insert后的id,还是线程A执行insert的id。
已知:
在数据库表自增的情况下,mybatis执行完一条insert sql后,Jdbc3KeyGenerator获取主键的逻辑,是在insert语句后获取最大的id。
场景:
线程A:插入insert语句先执行,然后执行Jdbc3KeyGenerator获取最大的id。
那么在线程A的Jdbc3KeyGenerator获取最大id前一时刻,线程B:又有insert语句执行了。
问:
那线程A获取到的id是线程B执行insert后的id,还是线程A执行insert的id。
LAST_INSERT_ID()
是按连接维护的,不同的请求相互之间不会受影响,线程A拿到的就是线程A的INSERT ID
。如果使用SELECT MAX(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. https://dev.mysql.com/doc/ref...
Snowflake
生成主键ID插入,提前获知ID
15 回答8.4k 阅读
8 回答6.2k 阅读
4 回答4.4k 阅读✓ 已解决
5 回答3.2k 阅读✓ 已解决
4 回答4k 阅读
1 回答3k 阅读✓ 已解决
3 回答3.6k 阅读✓ 已解决
不说数据库的情况下,就默认你是mysql吧,对于获取自增id的场景,mysql是插入完以后执行select LAST_INSERT_ID()就能获取到最大id(oracle在插入前,这点不同),mybatis做的也就是这样了,至于获取具体的数值,那是数据库对于这个的处理,特意试了下,开启事务以后,A事务先插入,但是不查询LAST_INSERT_ID(),B事务一次操作完,在B事务不管是否提交,A执行select LAST_INSERT_ID都是获取自己插入后的id,并没有串到B的id