4

我在通过 mod_wsgi 在 Linux 机器上运行的 Python 2.7 上遇到 SQL Alchemy 0.8.1 的问题。它有时会产生“InvalidRequestError:此会话处于'准备'状态;在此事务中不能发出进一步的 SQL”。

以下是错误的详细堆栈跟踪:


...
 File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2173, in one
    ret = list(self)
                self = <sqlalchemy.orm.query.Query object at 0x7f6f64348dd0>

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2216, in __iter__
    return self._execute_and_instances(context)
                self = <sqlalchemy.orm.query.Query object at 0x7f6f64348dd0>
             context = <sqlalchemy.orm.query.QueryContext object at 0x7f6f64348d50>

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2229, in _execute_and_instances
    close_with_result=True)
        querycontext = <sqlalchemy.orm.query.QueryContext object at 0x7f6f64348d50>
                self = <sqlalchemy.orm.query.Query object at 0x7f6f64348dd0>

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2220, in _connection_from_session
    **kw)
                self = <sqlalchemy.orm.query.Query object at 0x7f6f64348dd0>
                  kw = {'clause': , 'close_with_result': True, 'mapper': }

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 798, in connection
    close_with_result=close_with_result)
              mapper = 
              clause = <sqlalchemy.sql.expression.Select at 0x7f6f64348d90; Select object>
                self = <sqlalchemy.orm.session.Session object at 0x7f6f68f6fe50>
                  kw = {}
                bind = Engine(mysql+mysqldb://xxx:yyy@localhost/zzz?charset=utf8)
   close_with_result = True

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 802, in _connection_for_bind
    return self.transaction._connection_for_bind(engine)
              engine = Engine(mysql+mysqldb://xxx:yyy@localhost/zzz?charset=utf8)
                self = <sqlalchemy.orm.session.Session object at 0x7f6f68f6fe50>
              kwargs = {'close_with_result': True}

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 281, in _connection_for_bind
    self._assert_active()
                bind = Engine(mysql+mysqldb://xxx:yyy@localhost/zzz?charset=utf8)
                self = <sqlalchemy.orm.session.SessionTransaction object at 0x7f6f69be8050>

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 181, in _assert_active
    "This session is in 'prepared' state; no further "
         rollback_ok = False
         prepared_ok = False
          closed_msg = 'This transaction is closed'
                self = <sqlalchemy.orm.session.SessionTransaction object at 0x7f6f69be8050>

InvalidRequestError: This session is in 'prepared' state; no further SQL can be emitted within this transaction.

此错误随机发生 - 可能需要几天时间才能看到,也可能在 5 分钟后发生。我不明白是什么导致了这个问题和最大的问题——我不明白这个错误是什么意思。什么是“准备”状态?该文档没有关于此状态的信息。据我所见,它与双向交易有一些关系,但我没有在我的代码中使用它们。我使用 Session.query 和 Session.commit。

任何帮助,将不胜感激。

4

1 回答 1

1

我真的在这里猜测,但我认为您可能正在使用带有 wsgi 部署的线程,并且您的会话实例不是线程本地的。您当然可以这样做,只需按照官方文档中的简短教程进行操作即可:

上下文/线程本地会话

于 2013-07-11T18:42:37.237 回答