Added more context for errorcode 1213

There was not enough context to appropriately figure out if the
exception was logged due to 3 attempts failing beforehand.

Attempt limit was increased to 5

Affects issues:
- Close #1522
This commit is contained in:
Risto Lahtela 2020-08-08 17:31:22 +03:00
parent b2b983b05a
commit 01ffe8cbe1
2 changed files with 10 additions and 3 deletions

View File

@ -128,6 +128,9 @@ public class DBOpException extends IllegalStateException implements ExceptionWit
context.related("Missing privilege")
.whatToDo("Grant the required privileges to your MySQL user (often 'REFERENCES' privilege is missing).");
break;
case 1213:
context.related("Deadlock");
break;
default:
context.related("Unknown SQL Error code");
}

View File

@ -21,6 +21,7 @@ import com.djrapitops.plan.storage.database.DBType;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.SQLDB;
import com.djrapitops.plan.storage.database.queries.Query;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plugin.utilities.Verify;
import java.sql.*;
@ -37,7 +38,7 @@ public abstract class Transaction {
// SQLite version on 1.8.8 does not support savepoints, see createSavePoint() method
private static final AtomicBoolean SUPPORTS_SAVE_POINTS = new AtomicBoolean(true);
// Limit for Deadlock attempts.
private static final int ATTEMPT_LIMIT = 3;
private static final int ATTEMPT_LIMIT = 5;
private SQLDB db;
protected DBType dbType;
@ -87,7 +88,8 @@ public abstract class Transaction {
int errorCode = statementFail.getErrorCode();
boolean mySQLDeadlock = dbType == DBType.MYSQL && errorCode == 1213;
boolean h2Deadlock = dbType == DBType.H2 && errorCode == 40001;
boolean deadlocked = mySQLDeadlock || h2Deadlock || statementFail instanceof SQLTransactionRollbackException;
boolean deadlocked = mySQLDeadlock || h2Deadlock
|| statementFail instanceof SQLTransactionRollbackException;
if (deadlocked && attempts < ATTEMPT_LIMIT) {
executeTransaction(db); // Recurse to attempt again.
return;
@ -96,7 +98,9 @@ public abstract class Transaction {
failMsg += " (Attempted " + attempts + " times)";
}
throw new DBOpException(failMsg + rollbackStatusMsg, statementFail);
throw new DBOpException(failMsg + rollbackStatusMsg, statementFail, ErrorContext.builder()
.related("Attempts: " + attempts)
.build());
}
private String rollbackTransaction() {