Skip to content
Browse files

lock only if there is a cache enabled

  • Loading branch information...
1 parent 5a1cfa7 commit edd9c3c1dc6912682d2393a7382a7f2769a8f8cd @simonetripodi simonetripodi committed
Showing with 12 additions and 3 deletions.
  1. +12 −3 src/main/java/org/nnsoft/sameas4j/SameAsServiceImpl.java
View
15 src/main/java/org/nnsoft/sameas4j/SameAsServiceImpl.java
@@ -61,6 +61,11 @@
private final GsonBuilder gsonBuilder = new GsonBuilder();
/**
+ * Cache lock.
+ */
+ private final Lock lock = new ReentrantLock();
+
+ /**
* The {@code Cache} reference, can be null.
*/
private Cache cache;
@@ -116,8 +121,10 @@ public EquivalenceList getDuplicates(String keyword) throws SameAsServiceExcepti
URLConnection connection = null;
Reader reader = null;
- Lock lock = new ReentrantLock();
- lock.lock();
+ if (this.cache != null) {
+ lock.lock();
+ }
+
try {
connection = url.openConnection();
long lastModified = connection.getLastModified();
@@ -151,7 +158,9 @@ public EquivalenceList getDuplicates(String keyword) throws SameAsServiceExcepti
} catch (JsonParseException e) {
throw new SameAsServiceException("An error occurred while parsing the JSON response", e);
} finally {
- lock.unlock();
+ if (this.cache != null) {
+ lock.unlock();
+ }
if (connection != null && connection instanceof HttpURLConnection) {
((HttpURLConnection) connection).disconnect();

0 comments on commit edd9c3c

Please sign in to comment.
Something went wrong with that request. Please try again.