SQLAlchemy 核心 + 金字塔不关闭连接
SQLAlchemy core + Pyramid not closing connections
我有 SQLAlchemy CORE 1.0.9 和 Pyramid 框架 1.7。我正在使用以下配置连接到 postgres 9.4 数据库:
# file __ini__.py
from .factories import root_factory
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application."""
config = Configurator(settings=settings, root_factory=root_factory)
engine = engine_from_config(settings, prefix='sqlalchemy.')
# Retrieves database connection
def get_db(request):
connection = engine.connect()
def disconnect(request):
connection.close()
request.add_finished_callback(disconnect)
return connection
config.add_request_method(get_db, 'db', reify=True)
config.scan()
return config.make_wsgi_app()
使用该应用几个小时后,我开始收到以下错误:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: remaining connection slots are reserved for non-replication superuser connections
显然我已达到最大连接数。似乎 connections.close() 并没有真正关闭连接,只是 returns 连接到池。我知道我可以使用 NullPool 来禁用池,但这可能会对性能产生巨大影响。
有人知道配置 SQLAlchemy Core 以获得良好性能并正确关闭连接的正确方法吗?
请避免发送链接至 pyramid tutorials。我对 SQLAlchemy ORM 设置不感兴趣。请仅 SQLAlchemy Core。
实际上在之前的设置中一切都很好。问题是由于 Celery worker 没有关闭连接引起的。
我有 SQLAlchemy CORE 1.0.9 和 Pyramid 框架 1.7。我正在使用以下配置连接到 postgres 9.4 数据库:
# file __ini__.py
from .factories import root_factory
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application."""
config = Configurator(settings=settings, root_factory=root_factory)
engine = engine_from_config(settings, prefix='sqlalchemy.')
# Retrieves database connection
def get_db(request):
connection = engine.connect()
def disconnect(request):
connection.close()
request.add_finished_callback(disconnect)
return connection
config.add_request_method(get_db, 'db', reify=True)
config.scan()
return config.make_wsgi_app()
使用该应用几个小时后,我开始收到以下错误:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: remaining connection slots are reserved for non-replication superuser connections
显然我已达到最大连接数。似乎 connections.close() 并没有真正关闭连接,只是 returns 连接到池。我知道我可以使用 NullPool 来禁用池,但这可能会对性能产生巨大影响。
有人知道配置 SQLAlchemy Core 以获得良好性能并正确关闭连接的正确方法吗?
请避免发送链接至 pyramid tutorials。我对 SQLAlchemy ORM 设置不感兴趣。请仅 SQLAlchemy Core。
实际上在之前的设置中一切都很好。问题是由于 Celery worker 没有关闭连接引起的。