gpt4 book ai didi

python - MySQL、RDS、锁定​​超时

转载 作者:行者123 更新时间:2023-11-29 22:45:42 25 4
gpt4 key购买 nike

我正在创建一个使用 RDS MySQL 实例的应用程序,由节点和 python 程序访问。 Node 端很好,直到几天前,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')

奇怪的是,这个错误相当间歇性,并且不会总是达到同一点。有时会出现尝试提交,而其他时候则永远不会通过cursor.execute()

我还有另外 2 个类似的查询,只是更新各种计数器。有时它会毫无问题地执行所有 3 个任务,有时会卡在其中一个任务上。这个应用程序不应该有任何并发​​问题,它花费大约 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 为空白,没有文本...

如何进一步调试这个?我不知道还应该看什么。

最佳答案

我通过简单地在每个事务后关闭连接,然后为下一个数据库交互重新打开一个新事务来解决这个问题。不确定问题的最初原因是什么,但这似乎现在有效。

关于python - MySQL、RDS、锁定​​超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29156404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com