I'm doing some work using Python to update a MySQL database. Many instances of my script can be running at one time, so occasionally I will get LOCK error messages from the MySQL server:
mysql.connector.errors.DatabaseError: 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
If these occur, I would like to retry the operation. I can use a basic except Exception as e statement, but that traps all errors, which is not what I need.
How can I trap only the LOCK errors generated by MySQL?
cursor = mysql.connector.connect(host="myhost.com", user=username, passwd=password, db=schema, auth_plugin='mysql_native_password')
sql = "UPDATE table SET mycolumn = 'mydata'"
while True:
try:
cursor.execute(sql)
break
except Exception as e: # I only want to catch LOCK errors here
print(repr(e))
except:
print(sql)
raise