【发布时间】:2015-03-19 22:48:21
【问题描述】:
我正在创建一个使用 RDS 的 MySQL 实例的应用程序,由节点和 python 程序访问。节点端很好,直到几天前,python 端也很好。现在 python 程序在简单查询上失败了:
我正在通过上下文管理器运行查询,如下所示:
with safeExecute(dbConnection.cursor()) as cursor:
log('about to run SQL')
sql = """UPDATE ts
SET ts.fails = ts.fails + 1,
ts.attempts = ts.attempts + 1
WHERE ts.id = 206 """
cursor.execute(sql)
log('sql executed')
@contextmanager
def safeExecute(cursor):
try:
yield cursor
log('attempting to commit')
dbConnection.commit()
log('committed')
except MySQLdb.OperationalError as e:
dbConnection.rollback()
log("--CRITICAL--")
traceback.print_exc()
sys.exit(1)
except Exception as e:
dbConnection.rollback()
log("Caught DB exception: ")
traceback.print_exc()
sys.stdout.flush()
finally:
cursor.close()
控制台日志如下所示:
about to run sql
sql executed
attempting to commit
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
OR
OperationalError: (2006, 'MySQL server has gone away')
奇怪的是,这个错误是相当间歇的,并且不会总是到达同一点。有时会出现attempting to commit,而有时它永远不会超过cursor.execute()。
我还有两个类似的查询,只是更新了各种计数器。它有时会毫无问题地执行所有三个,有时会卡在其中一个上。这个应用程序不应该有任何并发问题,它会花费大约 30 秒的时间处理,然后按顺序运行上面的这些查询(因此不应该有任何竞争条件)。
但是,查看打开的表,它似乎没有被锁定(当我执行以下操作时查询正在运行)
mysql> show open tables where in_use>0;
+------------+------------+--------+-------------+
| Database | Table | In_use | Name_locked |
+------------+------------+--------+-------------+
| tearsheets | tearsheets | 1 | 0 |
+------------+------------+--------+-------------+
Show processlist 也没有透露任何信息。实际上它表明连接正在休眠:
ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
1230255 | <username> | <ip-address> | <ts table> | Sleep | 42 | | NULL
state 是空白的,那里没有文字...
我该如何进一步调试呢?我不知道还能看什么。
【问题讨论】:
-
附带说明,如果您使用的是 innodb,那么我强烈建议您使用 innotop 查看锁定信息。当锁被其他事务阻止时,它将显示涉及哪些事务。
标签: python mysql amazon-rds