回滚后,自增ID仍然增加。

比如当前ID是7,插入一条数据后,又回滚了。
然后你再插入一条数据,此时插入成功,这时候你的ID不是8,而是9.
因为虽然你之前插入回滚,但是ID还是自增了。

如果你认为自增ID不应该被事务化,那么其他事务不得不等待着,检查自增ID是被使用还是被回滚,这就导致阻塞。比如下面的例子,A表使用自增ID。
User 1
------------
begin transaction
insert into A ...
insert into B ...
update C ...
insert into D ...
commit

User 2
-----------
begin transaction
insert into A ...
insert into B ...
commit

看以上的例子代码,如果自增ID也要被事务化,那么假设user 2 的事务在user 1执行后的1毫秒后执行,那么他的插入到A表不得不等待User 1的整个事务结束,检查第一个自增ID是不是被使用了。这就导致阻塞。

自增ID不被事务化是设计使然,不是bug,如果需要紧密连续的自增序列,建议采用其他方法生成。

--不連續沒關係,需要時候生成一列
if object_id(tb)is not null drop table tb
go
create table tb(ID int )
insert tb select 1
insert tb select 2
insert tb select 5
insert tb select 18
insert tb select 13
select id ,[newid]=(select count(*) from tb where id<=t.id) from tb t order by [newid]
/*id          newid       
----------- ----------- 
1           1
2           2
5           3
13          4
18          5

(影響 5 個資料列)
*/

参考:http://www.jb51.net/article/27138.htm

事务回滚后,自增ID仍然增加的更多相关文章

  1. 为什么mysql事务回滚后, 自增ID依然自增

    事务回滚后,自增ID仍然增加,回滚后,自增ID仍然增加.比如当前ID是7,插入一条数据后,又回滚了.然后你再插入一条数据,此时插入成功,这时候你的ID不是8,而是9.因为虽然你之前插入回滚,但是ID还 ...

  2. Avoid catching exceptions inside atomic! You may need to manually revert model state when rolling back a transaction. 避免异常程序不抛错误 回滚 导致 自增id不连续。

    https://docs.djangoproject.com/en/3.0/topics/db/transactions/ You may need to manually revert model ...

  3. SQL Server事务回滚对自增键的影响

    SQL Server事务回滚时是删除原先插入导致的自增值,也就是回滚之前你你插入一条数据导致自增键加1,回滚之后还是加1的状态 --如果获取当前操作最后插入的identity列的值:select @@ ...

  4. 为什么mysql事务回滚后,自增ID依然自增

    因为innodb的auto_increament的计数器记录的当前值是保存在存内 存中的,并不是存在于磁盘上,当mysqlserver处于运行的时候,这个计数值只会随着insert改增长,不会随着de ...

  5. php+mysql实现事务回滚

    模拟条件:第一个表插入成功,但是第二个表插入失败,回滚.第一个表插入成功,第二个表插入成功,执行.第一个表插入失败,第二个表插入成功,回滚.第一个表插入失败,第二个表插入失败,回滚.以上情况都需要回滚 ...

  6. Try-Catch包裹的代码异常后,竟然导致了产线事务回滚!

    导读:​一段被try-catch包裹后的代码在产线稳定运行了200天后忽然发生了异常,而这个异常竟然导致了产线事务回滚.这期间究竟发生了什么?日常在项目过程中该如何避免事务异常?就在这个时候,老板拿着 ...

  7. [SQL]事务回滚详解及示例

    存储过程中的 SET XACT_ABORT ON 和事务 在存储过程中写SET XACT_ABORT ON 有什么用? SET XACT_ABORT ON是设置事务回滚的! 当为ON时,如果你存储中的 ...

  8. Spring异常抛出触发事务回滚

    Spring.EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚 /** * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*) ...

  9. SqlServer事务回滚(2)

    SQL Server 2008中SQL应用系列--目录索引 SQL事务 一.事务概念    事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是 ...

随机推荐

  1. mysql 慢查询开启

    相关博客: linux下开启mysql慢查询,分析查询语句 开启方法: 方法一:使用命令开启慢查询开启 mysql> show variables like "%long%" ...

  2. C# 或 JQuery导出Excel

    首先要添加NPOI.dll文件 然后添加类:NPOIHelper.cs using System; using System.Data; using System.Configuration; usi ...

  3. 学习Linux第五天

    1.VIM编辑器 3种模式: Command Model , Insert Model , Last line Model 安装vim: sudo apt-get install vim 如果提示出错 ...

  4. poj 1815 Friendship 字典序最小+最小割

    题目链接:http://poj.org/problem?id=1815 In modern society, each person has his own friends. Since all th ...

  5. Build Simple HTTP server

    1. The server just support POST&PUT method 2. It is a Python server, and save upload files in sp ...

  6. flex Chrome flash调试时 出现Shockwave flash has crashed的解决办法

    在Chrome中输入:chrome://plugins/     PPAPI的Flash Player停用. 使用NPAPI的Flash player. 这里好像没有显示是Debug版本. 但是我在调 ...

  7. 2014年03月09日攻击百度贴吧的XSS蠕虫源码

    var n=PageData.user.user_forum_list.info.length; var num=0; var config = { titles: ["\u4f60\u76 ...

  8. vuforia 结合 unity3d 开发 AR 的 androidAPP 总结

    原地址:https://software.intel.com/zh-cn/blogs/2014/07/09/vuforia-unity3d-ar-androidapp/?utm_campaign=CS ...

  9. Sql注入一种dump所有数据的方法

    Select exp(~(select*from(select(concat(@:=0,(select count(*)from`information_schema`.columns where t ...

  10. jQuery scroll事件

    scroll事件适用于window对象,但也可滚动iframe框架与CSS overflow属性设置为scroll的元素. $(document).ready(function () { //本人习惯 ...