0

I want to re-execute the query whenever SQLException occurs and sometime I get the Exception of

SQLException occurred... com.mysql.jdbc.exceptions.jdbc4.
MySQLTransactionRollbackExceptionDeadlock
found when trying to get lock; try restarting transaction

My code is below. The database is MySQL InnoDB .. Please suggest...

  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();
    }        
            }
4

2 回答 2

1
UPDATE tbl_users SET abill=billpush, asms ='2' WHERE uid='uid' AND sms='2' and asms<>'2'

这将防止死锁,只需在最后添加一个条件 asms<>'2'。
希望这能解决问题。

于 2013-05-15T12:52:20.053 回答
0

只需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
    }
}
于 2013-05-15T12:44:27.077 回答