mysql批量更新出错

### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
; SQL []; Deadlock found when trying to get lock; try restarting transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
2014-06-09 08:00:01,176 [] ERROR core.JobRunShell - Job DEFAULT.adSspDataTask threw an unhandled Exception:
java.lang.RuntimeException: 11org.springframework.dao.DeadlockLoserDataAccessException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
问题的具体描述在:http://www.iteye.com/problems/101887

这个错误是由于你用了事物,导致表的死锁。 死锁的原因很多,如果你的批量插入有依赖性的话,可能在Mysql里面形成了脏读。
第一步先解决你的死锁问题,用指令show engine innodb status \G 查看是那一个查询进程锁表, 找出来ID,杀掉它。
第二步检查自己的程序的SQL语言,一般批量插入的话尽量用insert into (),(),().... 一个SQL来执行,如果需要检查是否存在然后插入或替换,如果表有唯一索引或主键,用replace, 其他格式一样。
第三步检查你的访问数据库的逻辑,在什么时候进行的commit,你在读写的时候是否设定了独占式访问方法, 或数据库中已经其他的存储过程或触发器同时在操作这个表。

如果以上都无效,你最好是通过上述指令同时监控你的程序并在mysql里面用show process list查看是哪一个ID出了问题。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-14
创建一个表:createtableusers(usernamevarchar(15)notnullPrimaryKey,userpassvarchar(15)notnull);插入一条记录:insertintousersvalues('user1','pass1');再创建一个表:createtableroles(usernamevarchar(15)notnullreferencesusers(username),rolenamevarchar(15));再插入一条记录:insertintorolesvalues('dd','ddd');安分驷皋雁皓孟菑员缴功恪简歧悌燕烁邰战舆颟本回答被网友采纳
第2个回答  2014-06-09
我完全明白你的意思,不过我真没见过这种用法还是老老实实在程序里逐个更新,或者用游标(mysql5.0以上版本才支持)吧本回答被提问者采纳
相似回答