2016-09-25 53 views
0

我有SQLAlchemy核心1.0.9與金字塔框架1.7。我現在用的是下面的配置用於連接到一個Postgres 9.4數據庫:SQLAlchemy核心+金字塔不關閉連接

# 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()並沒有真正關閉連接,只是返回連接池。我知道我可以使用NullPool禁用池,但可能會對性能產生巨大影響。

有人知道正確配置SQLAlchemy Core以獲得良好性能並正確關閉連接嗎?

請不要發送鏈接到pyramid tutorials。我是對SQLAlchemy ORM設置不感興趣。請僅使用SQLAlchemy Core

+0

我認爲你應該使用Zope事務管理器(pyramid_tm)。這是一個與您的金字塔應用程序相關的包裝,與請求處理集成在一起。如果請求完成而沒有發生事件,它會自動提交事務;或者,如果出現異常,它會中止交易。 [更多信息](http://pyramid-sqlalchemy.readthedocs.io/en/latest/transactions.html) – webjunkie

+0

FWIW我沒有看到你的示例代碼有什麼問題。 ORM具有'Session.remove'來確保事物被刪除,但是使用SQLA核心,'.close'應該可以工作。 –

回答

0

其實一切都很好,在以前的設置。問題是由芹菜工人沒有關閉連接引起的。