Java 发生异常时重新执行数据库更新查询
每当发生SQLException时,我想重新执行查询,有时我会得到Java 发生异常时重新执行数据库更新查询,java,mysql,sql,Java,Mysql,Sql,每当发生SQLException时,我想重新执行查询,有时我会得到 SQLException occurred... com.mysql.jdbc.exceptions.jdbc4. MySQLTransactionRollbackExceptionDeadlock found when trying to get lock; try restarting transaction 我的代码如下。数据库是MySQL InnoDB。。请建议 String sqlquery = "UPDATE
SQLException occurred... com.mysql.jdbc.exceptions.jdbc4.
MySQLTransactionRollbackExceptionDeadlock
found when trying to get lock; try restarting transaction
我的代码如下。数据库是MySQL InnoDB。。请建议
String sqlquery = "UPDATE tbl_users SET abill=?"
+ " WHERE uid=? AND sms='2'";
PreparedStatement preStatement=null;
try{
con.setAutoCommit(false);
preStatement=con.prepareStatement(sqlquery);
preStatement.setString(1,billpush);
preStatement.setString(2,uid);
preStatement.executeUpdate();
con.commit();
}
catch(SQLException sE)
{
log.error("SQLException occurred... "+sE);
con.rollback();
}
finally {
if (preStatement != null) {
preStatement.close();
}
}
危险操作后,只需将标志设置为
true
:
bool ok = false;
while (!ok) {
ok = false;
try {
doSomethingFishy();
ok = true;
}
catch(Exception e) {
dealWithError(); // perhaps wait for a short while and increment a "retry" counter
}
}
updatetbl\u用户设置abill=billphush,asms='2',其中uid='uid'和sms='2'以及asms'2'
这将防止死锁,只需在末尾添加一个条件作为asms'2'
希望这能解决问题。查询中有一个循环。。当为2时,更新为2。。希望你得到它。。。僵局局势。因此终止..@SenthilPrabhu。。很抱歉当它是2时,更新为2??哪个循环?更新tbl_用户设置abill=billpush,asms=2“其中uid=“uid”和sms='2';你明白了吗??尝试使用不同的输入,它将工作..@SenthilPrabhu,但我需要的条件是sms=2。我应该更新他的记录,说短信=2,如果他没有通过账单。否则他的记录将被更新为sms=1如果他成功请理解逻辑不要用1更新1。。我的问题是,如果值是2,为什么在它已经是2的情况下再次更新为2//例如:你是说
Madan
名字改成Madan
。。因此,数据库进行无限循环
UPDATE tbl_users SET abill=billpush, asms ='2' WHERE uid='uid' AND sms='2' and asms<>'2'