SQLAlchemy查询偶尔会抛出StatementError

时间:2015-06-18 21:43:52

标签: python mysql flask sqlalchemy

我有一个Flask应用程序,它使用连接到我的生产服务器(Ubuntu机器)上运行的mysql数据库的SQLAlchemy。

75%的时间我发出某个POST请求,应用程序使用以下堆栈跟踪引发StatementError。

  [2015-06-18 17:25:46,512] {/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/flask/app.py:1423} ERROR - Exception on /conjugate/conjugate [POST]
Traceback (most recent call last):
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "./conjugate.py", line 169, in conjugate
    for translation in get_translations(lang, verb)],
  File "./conjugate.py", line 80, in get_translations
    for entry in session.query(Translation).filter_by(lang=lang, english=english):
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2515, in __iter__
    return self._execute_and_instances(context)
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2530, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 914, in execute
    return meth(self, multiparams, params)
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 323, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1010, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1078, in _execute_context
    None, None)
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1339, in _handle_dbapi_exception
    exc_info
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1071, in _execute_context
    conn = self._revalidate_connection()
  File "/home/dsaltares/conjugate/env/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 391, in _revalidate_connection
    "Can't reconnect until invalid "
StatementError: (sqlalchemy.exc.InvalidRequestError) Can't reconnect until invalid transaction is rolled back [SQL: u'SELECT translations.lang AS translations_lang, translat    ions.verb AS translations_verb, translations.english AS translations_english, translations.description AS translations_description \nFROM translations \nWHERE translations.l    ang = %s AND translations.english = %s'] [parameters: [immutabledict({})]]

剩下的25%查询工作正常,它返回预期的数据。这些查询在时间上是间隔的,并不像任何人正在破坏服务。

查询正在崩溃

try:
    for entry in session.query(Translation).filter_by(lang=lang, english=english):
        translations.append({
            'lang': lang,
            'english': english,
            'verb': entry.verb,
            'description': entry.description
        })
except:
    app.logger.error('Error querying translations for (%s, %s)\n%s' % (lang, english, traceback.format_exc()))
    raise

翻译映射器:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String

Base = declarative_base()

class Translation(Base):
    __tablename__ = 'translations'

    lang = Column(String(10), primary_key=True)
    verb = Column(String(25), primary_key=True)
    english = Column(String(25), primary_key=True)
    description = Column(String)

    def __repr__(self):
        return "<Translation(lang='%s', verb='%s', english='%s'>" % (self.lang, self.verb, self.english)

关于它可能是什么的任何想法?我需要提供更多信息吗?

0 个答案:

没有答案