From 4b9bbd779e6179ff240cfdc69e98de95b2705d68 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 18 Nov 2015 11:30:35 +0000 Subject: [PATCH] Removed reentrant lock using syncronized instead git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@120300 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../AccountingPersistenceBackendFactory.java | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java index 7d08097..5d4b8ca 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java @@ -9,7 +9,6 @@ import java.util.HashMap; import java.util.Map; import java.util.ServiceLoader; import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.ReentrantLock; import org.gcube.accounting.aggregation.scheduler.AggregationScheduler; import org.gcube.common.scope.api.ScopeProvider; @@ -31,7 +30,6 @@ public abstract class AccountingPersistenceBackendFactory { private static String fallbackLocation; - private static ReentrantLock lock = new ReentrantLock(); private static Map accountingPersistenceBackends; private static Map fallbackLastCheck; @@ -118,9 +116,9 @@ public abstract class AccountingPersistenceBackendFactory { if( (lastCheckTimestamp + FALLBACK_RETRY_TIME) <= now ){ logger.debug("The {} for scope {} is {}. Is time to rediscover if there is another possibility.", AccountingPersistenceBackend.class.getSimpleName(), scope, actual.getClass().getSimpleName()); + AccountingPersistenceBackend discoveredPersistenceBackend = discoverAccountingPersistenceBackend(scope); - try { - lock.lock(); + synchronized (accountingPersistenceBackends) { logger.trace("Renewing Last check Timestamp. The next one will be {}", now); Long reCheckTimestamp = fallbackLastCheck.get(scope); if(lastCheckTimestamp.compareTo(reCheckTimestamp)!=0){ @@ -129,11 +127,6 @@ public abstract class AccountingPersistenceBackendFactory { return actual; } fallbackLastCheck.put(scope, now); - lock.unlock(); - - AccountingPersistenceBackend discoveredPersistenceBackend = discoverAccountingPersistenceBackend(scope); - - lock.lock(); if(discoveredPersistenceBackend!=null){ // Passing the aggregator to the new AccountingPersistenceBackend // so that the buffered records will be persisted with the new method @@ -149,11 +142,8 @@ public abstract class AccountingPersistenceBackendFactory { actual.getClass().getSimpleName(), scope, discoveredPersistenceBackend.getClass().getSimpleName(), e); } - lock.unlock(); return discoveredPersistenceBackend; } - }finally { - lock.unlock(); } } @@ -175,9 +165,8 @@ public abstract class AccountingPersistenceBackendFactory { return createFallback(null); } - lock.lock(); AccountingPersistenceBackend persistence = null; - try { + synchronized (accountingPersistenceBackends) { persistence = accountingPersistenceBackends.get(scope); if(persistence==null){ /* @@ -193,8 +182,6 @@ public abstract class AccountingPersistenceBackendFactory { */ fallbackLastCheck.put(scope, ((now - FALLBACK_RETRY_TIME) - 1)); } - } finally { - lock.unlock(); } persistence = accountingPersistenceBackends.get(scope);