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,27 +567,27 @@ public class BlockChain {
if (now == null) if (now == null)
return; return;
try (final Repository repository = RepositoryManager.tryRepository()) { ReentrantLock blockchainLock = Controller.getInstance().getBlockchainLock();
if (repository == null) if (!blockchainLock.tryLock())
return; // Too busy to trim right now, try again later
return;
ReentrantLock blockchainLock = Controller.getInstance().getBlockchainLock(); try {
if (!blockchainLock.tryLock()) try (final Repository repository = RepositoryManager.tryRepository()) {
// Too busy to trim right now, try again later if (repository == null)
return; return;
try {
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 { } catch (DataException e) {
blockchainLock.unlock(); LOGGER.warn(String.format("Repository issue trying to trim old online accounts signatures: %s", e.getMessage()));
} }
} catch (DataException e) { } finally {
LOGGER.warn(String.format("Repository issue trying to trim old online accounts signatures: %s", e.getMessage())); blockchainLock.unlock();
} }
} }