Add synchronization around updating trim heights to prevent deadlock/rollback

This commit is contained in:
catbref 2020-11-04 10:01:20 +00:00
parent c125a53655
commit 16397852ae
3 changed files with 21 additions and 12 deletions

View File

@ -415,6 +415,9 @@ public class HSQLDBATRepository implements ATRepository {
@Override
public void setAtTrimHeight(int trimHeight) throws DataException {
// trimHeightsLock is to prevent concurrent update on DatabaseInfo
// that could result in "transaction rollback: serialization failure"
synchronized (this.repository.trimHeightsLock) {
String updateSql = "UPDATE DatabaseInfo SET AT_trim_height = ?";
try {
@ -424,6 +427,7 @@ public class HSQLDBATRepository implements ATRepository {
throw new DataException("Unable to set AT state trim height in repository", e);
}
}
}
@Override
public void prepareForAtStateTrimming() throws DataException {

View File

@ -477,6 +477,9 @@ public class HSQLDBBlockRepository implements BlockRepository {
@Override
public void setOnlineAccountsSignaturesTrimHeight(int trimHeight) throws DataException {
// trimHeightsLock is to prevent concurrent update on DatabaseInfo
// that could result in "transaction rollback: serialization failure"
synchronized (this.repository.trimHeightsLock) {
String updateSql = "UPDATE DatabaseInfo SET online_signatures_trim_height = ?";
try {
@ -486,6 +489,7 @@ public class HSQLDBBlockRepository implements BlockRepository {
throw new DataException("Unable to set online accounts signatures trim height in repository", e);
}
}
}
@Override
public int trimOldOnlineAccountsSignatures(int minHeight, int maxHeight) throws DataException {

View File

@ -60,6 +60,7 @@ public class HSQLDBRepository implements Repository {
protected List<String> sqlStatements;
protected long sessionId;
protected final Map<String, PreparedStatement> preparedStatementCache = new HashMap<>();
protected final Object trimHeightsLock = new Object();
private final ATRepository atRepository = new HSQLDBATRepository(this);
private final AccountRepository accountRepository = new HSQLDBAccountRepository(this);