问问题
1493 次
2 回答
1
而不是 while True,您可以尝试在 except 块中再次连接和提交。
def mysql_handling(string):
global cursor
try:
cursor.execute(string)
if 'SELECT' not in string:
db.commit()
except MySQLdb.MySQLError:
cursor.close()
print 'something went wrong!!'
time.sleep(1)
cursor = get_cursor()
cursor.execute(string)
if 'SELECT' not in string:
db.commit()
finally:
if cursor:
cursor.close()
或者你可以保持最大重试次数,比如 5 次。
于 2013-03-05T13:04:55.147 回答
1
从这个页面看来,您似乎无法真正捕获特定的异常,但需要尽可能接近(OperationalError)并检查 errno 以获取确切的错误代码;
except MySQLdb.OperationalError as err:
if err.errno == errorcode.CR_SERVER_LOST:
# This is the error you're looking for
else:
# This is not the error you're looking for
raise
于 2013-03-05T13:05:22.877 回答