简体   繁体   中英

SQLAlchemy idle in transaction

I have application writen Python 3.6, Flask and SQLAlchemy (PostgreSQL).

I encountered problems with hanging idle in transaction connections in my db. It's probably because I don't commit nor rollback after select queries.

I use default SQLALchemy configuration: db = SQLAlchemy()

Sample endpoint that creates hanging connections:

class Test(Resource):

    def get(self, pk):
        return User.query.get(pk).serialize()

What's the way of handling such select queries? Should I select then commit? Or select then rollback? or entirely close connection after request? But closing connection causes that on every request there will be new connection to database opened.

What's best way?

This article describes what's happening and how to deal with it: http://oddbird.net/2014/06/14/sqlalchemy-postgres-autocommit/

Short answer: SQLAlchemy defaults to implicitly opening a new transactions. You could either commit after every SELECT or turn on autocommit (read the article to learn more).

Here's an SO post on the matter.
sqlalchemy, postgresql and relationship stuck in "idle in transaction"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM