skip to Main Content

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.

2

Answers


  1. No, it is not true that enabling autocommit would commit evvery data modification statement separately.

    Mysql manual on START TRANSACTION, COMMIT, and ROLLBACK Statements says:

    To disable autocommit mode implicitly for a single series of
    statements, use the START TRANSACTION statement:

    With START
    TRANSACTION, autocommit remains disabled until you end the transaction
    with COMMIT or ROLLBACK.

    Start transaction statement is an alias for begin. So, when you start a new transaction explicitly, autocommit is disabled.

    Login or Signup to reply.
  2. 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).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search