Skip to content
Advertisement

Tag: transactions

difference between autobegin and Session.begin

In the following code snippet a commit is executed once the with block is exited. As per the docs https://docs.sqlalchemy.org/en/20/orm/session_api.html#sqlalchemy.orm.Session.begin The Session object features autobegin behavior, so that normally it is not necessary to call the Session.begin() method explicitly Running without begin, like so does not commit even though the docs mention autobegin is not needed. The docs for Session.begin

Python flask-sqlalchemy: Do I have to commit session after a query?

I am writing an app in python flask-sqlalchemy with MySQL DB (https://flask-sqlalchemy.palletsprojects.com/en/2.x/) and I am wondering if I have to make “db.session.commit() or db.session.rollback()” after GET call, which only query DB . For example: Answer orders = Order.query.all() is a SELECT query that could be extended to include additional filters (WHERE etc.). It doesn’t alter the database, it simply reads

How to test a Django on_commit hook without clearing the database?

The on_commit function has been added to Django 1.9 to be able to trigger an action (e.g. a Celery task) after the current transaction has committed. They mention later in the docs that one should use TransactionTestCase to test features that rely on that function. However, unlike TestCase (which uses transactions and rolls them back), TransactionTestCase empties the whole database

Atomic increment of a counter in django

I’m trying to atomically increment a simple counter in Django. My code looks like this: If I understand Django correctly, this should wrap the function in a transaction and make the increment atomic. But it doesn’t work and there is a race condition in the counter update. How can this code be made thread-safe? Answer Use an F expression: either

Advertisement