回滚事件只是让原数据看起来不变,但是id还是会自增吗?

 

回滚事件只是让原数据看起来不变,但是id还是会自增对吗?

http://img2.mukewang.com/5a58c8ff0001606511470572.jpg

http://img3.mukewang.com/5a58c90000018fea10970670.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import pymysql
#连接
conn = pymysql.connect(host="127.0.0.1",database="s8",user="root",password="123",charset="utf8")
 
#获取光标
cursor = conn.cursor()
 
#写sql语句
sql_insert = "insert into user1(username) value('name10');"
sql_update = "update user1 set username='name91' where userid=9;"
sql_delete = "delete from user1 where userid<3;"
 
#回滚事务:要么全执行,要么全部执行。
# 注意:新增
try:
    #执行语句
    cursor.execute(sql_insert)
    print(cursor.rowcount)
    print(cursor.lastrowid)  # 最后的id
 
    cursor.execute(sql_update)
    print(cursor.rowcount)
 
    cursor.execute(sql_delete)
    print(cursor.rowcount)
 
    #提交事务
    conn.commit()
except Exception as e:
    print(e)
    conn.rollback()
 
#关闭连接
cursor.close()
conn.close()

 

 

自增id如果也事务化会很容易导致阻塞 因此是设计使然 如果业务原因非要连续 可以不采用自增而是插入时通过count等手动赋id

因为innodb的auto_increament的计数器记录的当前值是保存在存内 存中的,并不是存在于磁盘上,当mysql 
server处于运行的时候,这个计数值只会随着insert改增长,不会随着delete而减少。而当mysql 
server启动时,当我们需要去查询auto_increment计数值时,mysql便会自动执行:SELECT MAX(id) FROM 表名 
FOR UPDATE;语句来获得当前auto_increment列的最大值,然后将这个值放到auto_increment计数器中。所以就算Rollback MySQL的auto_increament计数器也不会作负运算

 
posted @ 2019-10-12 18:34  那些年的代码  阅读(382)  评论(0编辑  收藏  举报