SQLAlchemy在事务中处于空闲状态 [英] SQLAlchemy idle in transaction

查看:103
本文介绍了SQLAlchemy在事务中处于空闲状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用Python 3.6,Flask和SQLAlchemy(PostgreSQL)编写的应用程序.

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

我在数据库中挂起idle in transaction连接时遇到问题.可能是因为我在选择查询后不提交也不回滚.

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.

我使用默认的SQLALchemy配置:db = SQLAlchemy()

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.

什么是最好的方法?

推荐答案

本文介绍了正在发生的事情以及如何对其进行处理: http://oddbird.net/2014/06/14/sqlalchemy-postgres-autocommit/

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

简短的回答:SQLAlchemy默认情况下隐式打开一个新事务.您可以在每次SELECT之后提交,也可以打开自动提交(阅读文章以了解更多信息).

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).

这是此事的特刊.
sqlalchemy,postgresql和关系卡在交易空闲"中

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

这篇关于SQLAlchemy在事务中处于空闲状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆