pythonmysqlpeeweeflask-peewee

Peewe upgrade causing weired autocommit behaviour


We currently use peewe 2.x and this is the code we use custom mysql connection:

from peewee import MySQLDatabase


class SQLDb(MySQLDatabase):
def connect(self, *args, **kwargs):
    attempts = 5
    while attempts > 0:
        # Logic to retry db connection
         
def begin(self):   
    self.execute_sql('set autocommit=0')
     self.execute_sql('begin')
 

Now I call the transaction using DB.tranaction() while runing the transaction.

Now once we upgraded the peewe to 3.17.x we are seeing weired behaviour.

with this being set as it is

 def begin(self):   
    self.execute_sql('set autocommit=0')
    self.execute_sql('begin')

When txn completes and mysql command run without txn it does not commit the data. When i remove this line self.execute_sql('set autocommit=0') it started working as expected.

Now if i remove self.execute_sql('set autocommit=0') i beleive i will be changing transaction behaviour to commit at every mysql command. How do i ensure that with peewe 3.17.x it works as expected?

It works find till peewe 3.15.3. Peewe 3.16 has some breakimng changes related to peewe autocomit behaviour.


Solution

  • The changelog describes the behavior change in 3.16.0: https://github.com/coleifer/peewee/releases/tag/3.16.0

    Starting in 3.16.0 we put the database driver into autocommit mode, but balance that with a functional begin() method (so you can presumably drop your override). Transactional semantics are thoroughly tested across Sqlite/MySQL/Postgres and are working properly.

    Top-level write queries that are executed outside of a transaction are autocommitted (though this was also the behavior in all previous versions).