Skip to content

Commit

Permalink
:enh: recognizing deadlock exceptions and reattempting trasnsaction i…
Browse files Browse the repository at this point in the history
…f they occour

Signed-off-by: dseurotech <[email protected]>
  • Loading branch information
dseurotech committed Jan 23, 2024
1 parent 52c3374 commit 735aa2e
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.util.KapuaExceptionUtils;
import org.eclipse.kapua.storage.TxContext;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -77,7 +78,20 @@ public KapuaException convertPersistenceException(Exception ex) {
return KapuaExceptionUtils.convertPersistenceException(ex);
}

private final Predicate isLockExceptionTester = t -> t instanceof OptimisticLockException || t instanceof PessimisticLockException;
//https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_lock_wait_timeout
private static final int ER_LOCK_WAIT_TIMEOUT = 1205;
//https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_lock_deadlock
private static final int ER_LOCK_DEADLOCK = 1213;
private final Predicate isLockExceptionTester = t -> {
if (t instanceof OptimisticLockException || t instanceof PessimisticLockException) {
return true;

Check warning on line 87 in commons/src/main/java/org/eclipse/kapua/commons/jpa/JpaTxContext.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/jpa/JpaTxContext.java#L87

Added line #L87 was not covered by tests
}
if (t instanceof DatabaseException) {
final DatabaseException de = (DatabaseException) t;

Check warning on line 90 in commons/src/main/java/org/eclipse/kapua/commons/jpa/JpaTxContext.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/jpa/JpaTxContext.java#L90

Added line #L90 was not covered by tests
return de.getErrorCode() == ER_LOCK_DEADLOCK || de.getErrorCode() == ER_LOCK_WAIT_TIMEOUT;
}
return false;

Check warning on line 93 in commons/src/main/java/org/eclipse/kapua/commons/jpa/JpaTxContext.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/jpa/JpaTxContext.java#L93

Added line #L93 was not covered by tests
};

@Override
public boolean isRecoverableException(Exception ex) {
Expand Down

0 comments on commit 735aa2e

Please sign in to comment.