SQLAlchemy连接错误

时间:2014-09-10 14:44:43

标签: python multithreading sqlalchemy

我遇到了一些奇怪的错误,这些错误似乎是由Sqlalchemy使用的连接造成的,我无法准确确定..我希望有人知道这里有什么线索。

我们正在使用Pyramid(版本1.5b1)并使用Sqlalchemy(版本0.9.6)进行所有数据库连接。有时我们会收到与数据库连接或会话相关的错误,大多数时候会出现cursor already closedThis Connection is closed错误,但我们也会得到其他相关的例外情况:

(OperationalError) connection pointer is NULL
(InterfaceError) cursor already closed
Parent instance <...> is not bound to a Session, and no contextual session is established; lazy load operation of attribute '...' cannot proceed

A conflicting state is already present in the identity map for key (<class '...'>, (1001L,))
This Connection is closed (original cause: ResourceClosedError: This Connection is closed)
(InterfaceError) cursor already closed
Parent instance <...> is not bound to a Session; lazy load operation of attribute '...' cannot proceed
Parent instance <...> is not bound to a Session, and no contextual session is established; lazy load operation of attribute '...' cannot proceed
'NoneType' object has no attribute 'twophase'
(OperationalError) connection pointer is NULL
This session is in 'prepared' state; no further

没有银弹可以重现它们,只有通过多次刷新它们必然会在某个时刻发生。因此,我使用多机制来制作一个脚本,同时发送不同的网址,并查看它发生的位置和时间。

似乎触发的网址并不重要,当存在跨越较长时间的并发请求(以及其他请求之间的服务)时会发生错误。这似乎表明存在某种线程问题;会话或连接在不同的线程之间共享。

在谷歌上搜索这些问题后,我发现了很多主题,其中大多数都告诉我们使用范围会话,但事情是我们已经使用它们了:

db_session = scoped_session(sessionmaker(extension=ZopeTransactionExtension(), autocommit=False, autoflush=False))
db_meta = MetaData()
  • 我们所有的orm对象都有一个BaseModel:

    BaseModel = declarative_base(cls = BaseModelObj,metaclass = BaseMeta,metadata = db_meta)

  • 我们使用pyramid_tm补间来处理请求期间的交易

  • 我们将db_session.remove()挂钩到金字塔NewResponse事件(在一切运行后触发)。我也尝试将它放在pyramid_tm之后运行的单独补间中,或者甚至根本不执行它,这些似乎都没有效果,所以响应事件似乎是最干净的地方。

  • 我们在金字塔项目的主要入口点创建引擎,并使用NullPool并将连接池保留到pgbouncer。我们还在这里为BaseModel配置会话和绑定:

    engine = engine_from_config(config.registry.settings,&#39; sqlalchemy。&#39;,poolclass = NullPool) db_session.configure(bind = engine,query_cls = FilterQuery) BaseModel.metadata.bind = engine config.add_subscriber(cleanup_db_session,NewResponse) return config.make_wsgi_app()

  • 在我们的应用程序中,我们使用:

    访问所有数据库操作 来自project.db的

    导入db_session ... db_session.query(为MyModel).filter(...) db_session.execute(...)

  • 我们使用psycopg2 == 2.5.2处理与pgbouncer之间的postgres的连接

  • 我确保没有对db_session或连接的引用保存在任何地方(这可能导致其他线程重用它们)

我还尝试使用不同的网络服务器进行垃圾邮件测试,使用女服务员和cogen我很容易得到错误,使用wsgiref我们毫不奇怪没有错误(单线程)。使用uwsgi和gunicorn(4名工人,gevent)我没有遇到任何错误。

鉴于所使用的网络服务器存在差异,我认为它要么与某些处理线程请求的Web服务器有关,要么使用新进程(可能是分叉问题)?更复杂的事情,当时间继续,我做了一些新的测试,问题已经在女服务员消失了,但现在发生了gunicorn(当使用gevent时)!我不知道如何调试这个...

最后,为了测试连接发生了什么,我在游标执行开始时将一个属性附加到连接,并尝试在执行结束时读取属性:

@event.listens_for(Engine, "before_cursor_execute")
def _before_cursor_execute(conn, cursor, stmt, params, context, execmany):
  conn.pdtb_start_timer = time.time()

@event.listens_for(Engine, "after_cursor_execute")
def _after_cursor_execute(conn, cursor, stmt, params, context, execmany):
  print conn.pdtb_start_timer

令人惊讶的是,这有时会引发异常:&#39; Connection&#39;对象没有属性&lt; pdtb_start_timer&#39;

让我感到非常奇怪......我发现了一个关于类似事情的讨论:https://groups.google.com/d/msg/sqlalchemy/GQZSjHAGkWM/rDflJvuyWnEJ 并尝试添加策略=&#39; threadlocal&#39;从我所理解的应该强制1连接的发动机到发动机。但它对我看到的错误没有任何影响..(除了一些单元测试失败,因为我需要两个不同的会话/连接进行一些测试,这迫使1个连接被关联)

有没有人知道这里可能会发生什么,或者有更多关于如何解决这个问题的指示?

提前致谢!

Matthijs Blaas

1 个答案:

答案 0 :(得分:1)

更新:由一个准备好的sql语句中发送的多个命令引起的错误。 Psycopg2似乎允许这样,但显然它可能会导致奇怪的问题。 PG8000连接器在多个命令上更加严格和保护,发送一个命令解决了问题!