Try for blockchain lock before tying up repository in BlockChain.trimOldOnlineAccountsSignatures

This commit is contained in:
catbref 2019-11-26 08:58:19 +00:00
parent 790f569dbd
commit 9b894616ee

View File

@ -567,28 +567,28 @@ public class BlockChain {
if (now == null) if (now == null)
return; return;
try (final Repository repository = RepositoryManager.tryRepository()) {
if (repository == null)
return;
ReentrantLock blockchainLock = Controller.getInstance().getBlockchainLock(); ReentrantLock blockchainLock = Controller.getInstance().getBlockchainLock();
if (!blockchainLock.tryLock()) if (!blockchainLock.tryLock())
// Too busy to trim right now, try again later // Too busy to trim right now, try again later
return; return;
try { try {
try (final Repository repository = RepositoryManager.tryRepository()) {
if (repository == null)
return;
int numBlocksTrimmed = repository.getBlockRepository().trimOldOnlineAccountsSignatures(now - BlockChain.getInstance().getOnlineAccountSignaturesMaxLifetime()); int numBlocksTrimmed = repository.getBlockRepository().trimOldOnlineAccountsSignatures(now - BlockChain.getInstance().getOnlineAccountSignaturesMaxLifetime());
if (numBlocksTrimmed > 0) if (numBlocksTrimmed > 0)
LOGGER.debug(String.format("Trimmed old online accounts signatures from %d block%s", numBlocksTrimmed, (numBlocksTrimmed != 1 ? "s" : ""))); LOGGER.debug(String.format("Trimmed old online accounts signatures from %d block%s", numBlocksTrimmed, (numBlocksTrimmed != 1 ? "s" : "")));
repository.saveChanges(); repository.saveChanges();
} finally {
blockchainLock.unlock();
}
} catch (DataException e) { } catch (DataException e) {
LOGGER.warn(String.format("Repository issue trying to trim old online accounts signatures: %s", e.getMessage())); LOGGER.warn(String.format("Repository issue trying to trim old online accounts signatures: %s", e.getMessage()));
} }
} finally {
blockchainLock.unlock();
}
} }
} }