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,13 +415,17 @@ public class HSQLDBATRepository implements ATRepository {
@Override
public void setAtTrimHeight(int trimHeight) throws DataException {
String updateSql = "UPDATE DatabaseInfo SET AT_trim_height = ?";
// 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 {
this.repository.executeCheckedUpdate(updateSql, trimHeight);
} catch (SQLException e) {
repository.examineException(e);
throw new DataException("Unable to set AT state trim height in repository", e);
try {
this.repository.executeCheckedUpdate(updateSql, trimHeight);
} catch (SQLException e) {
repository.examineException(e);
throw new DataException("Unable to set AT state trim height in repository", e);
}
}
}

View File

@ -477,13 +477,17 @@ public class HSQLDBBlockRepository implements BlockRepository {
@Override
public void setOnlineAccountsSignaturesTrimHeight(int trimHeight) throws DataException {
String updateSql = "UPDATE DatabaseInfo SET online_signatures_trim_height = ?";
// 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 {
this.repository.executeCheckedUpdate(updateSql, trimHeight);
} catch (SQLException e) {
repository.examineException(e);
throw new DataException("Unable to set online accounts signatures trim height in repository", e);
try {
this.repository.executeCheckedUpdate(updateSql, trimHeight);
} catch (SQLException e) {
repository.examineException(e);
throw new DataException("Unable to set online accounts signatures trim height in repository", e);
}
}
}

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);