看你的需要
mysql 的 innodb 反对多种事务隔离级别:
- READ UNCOMMITTED
- READ COMMITTED
- REPEATABLE READ
- SERIALIZABLE
默认的隔离级别是 REPEATABLE READ
pgsql 的默认的隔离级别是
READ COMMITTED
;sql server 默认是READ UNCOMMITTED
; sql server 默认是Read Committed
咱们应用 mysql 的默认事务隔离级别,又敞开了主动提交事务:SET AUTOCOMMIT = 0
。又凑巧,你应用 select 后不应用 commit。那你这辈子会只能读取到反复的数据。
比方上面一个表:
连贯 1,读取 id = 1 的 row
连贯 2,批改 id=1 的 row 的 name 为 pon
这个时候,连贯 1 再读取 id=1 的 row,你感觉 name 是什么?是 1 还是 pon?
答案是 1,而不是 pon
这很可怕
那要怎么能力读取到 pon 而不是 1 呢?
- 计划一:对于连贯 1,select 之后,指定 commit、roollback、begin 等等操作,来新开一个事务
- 计划二:调整事务隔离级别为
READ COMMITTED
import pymysql
import pymysql.cursors
from pymysql.connections import Connection
from loguru import logger
# Connect to the database
connection_1: Connection = pymysql.connect(host='192.168.31.245',
user='root',
password='xxxx',
port=3306,
database='seckill',
cursorclass=pymysql.cursors.DictCursor)
cursor_1 = connection_1.cursor()
connection_2: Connection = pymysql.connect(host='192.168.31.245',
user='root',
password='xxxx',
port=3306,
database='seckill',
cursorclass=pymysql.cursors.DictCursor)
cursor_2 = connection_2.cursor()
sql = """SELECT * FROM account WHERE id=1;""".strip()
cursor_1.execute(sql)
logger.debug(cursor_1.fetchall())
connection_1.commit()
# sql = """# DELETE FROM account where name='jike';
# """.strip()
sql = """UPDATE account SET name="pon"WHERE id=1;""".strip()
cursor_2.execute(sql)
connection_2.commit()
sql = """SELECT * FROM account WHERE id=1;""".strip()
cursor_1.execute(sql)
logger.debug(cursor_1.fetchall())