pymysql.err.InternalError: (1205, 'Lock wait timeout exceeded; try restarting transaction')错误处理

问题描述:

  在使用pymysql库时,利用游标执行插入操作,产生错误,会出现pymysql.err.InternalError: (1205, 'Lock wait timeout exceeded; try restarting transaction')的错误,此时产生了不必要的锁而锁住其他的操作。

   插入操作产生错误的原因有很多,我这里是因为主键有相同的值,其他的增删改可能也会因为错误产生死锁。

 

解决办法:

  我们可以用 try 来捕获异常,进行错误回滚,防止锁住其他操作,也可以产生错误时跳过当前的错误操作不执行并print一个错误提示出来

1     curosr_1 = conn.cursor()
2 
3     try:
4       curosr_1.execute("insert IGNORE into lgjob(job_name,salary,work_years,degree_need,job_type,job_url) values('b','b','c','d','e','f') ")
5       conn.commit()
6     except Exception as e:
7          # 错误回滚
8          conn.rollback()
原文地址:https://www.cnblogs.com/jyroy/p/9439436.html