I am using mysql 8.0, using the innodb engine, using the version of peewee 3.14, I use peewee transaction commit, without executing commit, I saw the data has been written into the database, but I did not execute db.commit()
, I manually execute commands in MySQL. If I don’t execute commit, I won’t be able to write. However, there will be inconsistencies when using peewee transaction commit. What is wrong with my code?
db =db = MySQLDatabase("address", host="127.0.0.1", port=3306, user="root", passwd="xxx",autocommit=False,autorollback=True)
with db.manual_commit():
db.begin()
Spead.create(dis="test",number="test",value=333)
You also opened an issue on the peewee issue tracker, where I've responded with the following comment:
I saw the data has been written into the database
Assuming you mean you were using a separate connection and saw the data before it had been committed by the peewee connection? You might check your read isolation settings.
When I run the following script I get the expected output -- the 2nd connection does not see the uncommitted row until I call commit:
from peewee import *
db = MySQLDatabase('peewee_test')
db2 = MySQLDatabase('peewee_test') # 2nd connection
class Reg(Model):
key = TextField()
class Meta:
database = db
class Reg2(Reg): # model class for accessing table using 2nd conn
class Meta:
database = db2
table_name = 'reg'
db.create_tables([Reg])
with db.manual_commit() as tx:
db.begin()
Reg.create(key='k1') # create a row using first conn
db2.connect() # query table using 2nd conn
print('is "k1" visible to conn2 BEFORE commit?')
print('rows in "reg" table: %s' % Reg2.select().count())
db.commit()
print('is "k1" visible to conn2 AFTER commit?')
print('rows in "reg" table: %s' % Reg2.select().count())
db2.close()
db.drop_tables([Reg])
Output:
is "k1" visible to conn2 BEFORE commit?
rows in "reg" table: 0
is "k1" visible to conn2 AFTER commit?
rows in "reg" table: 1