aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/database/SimpleObjectManager.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-02-22 11:34:57 -0800
committerAnton Tananaev <anton.tananaev@gmail.com>2020-02-22 11:34:57 -0800
commitdeb519ebd6798450509afaf4067e140edd7eb0d0 (patch)
treee93d94d51245355e2b43c3cb3aedc77e169f0f09 /src/main/java/org/traccar/database/SimpleObjectManager.java
parentcb25fe3e58c0876fe6c000a1f0a7c7435e3fb0e1 (diff)
downloadtrackermap-server-deb519ebd6798450509afaf4067e140edd7eb0d0.tar.gz
trackermap-server-deb519ebd6798450509afaf4067e140edd7eb0d0.tar.bz2
trackermap-server-deb519ebd6798450509afaf4067e140edd7eb0d0.zip
Fix concurrency issues
Diffstat (limited to 'src/main/java/org/traccar/database/SimpleObjectManager.java')
-rw-r--r--src/main/java/org/traccar/database/SimpleObjectManager.java30
1 files changed, 18 insertions, 12 deletions
diff --git a/src/main/java/org/traccar/database/SimpleObjectManager.java b/src/main/java/org/traccar/database/SimpleObjectManager.java
index 15dda4520..eb8284d4e 100644
--- a/src/main/java/org/traccar/database/SimpleObjectManager.java
+++ b/src/main/java/org/traccar/database/SimpleObjectManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -42,16 +42,22 @@ public abstract class SimpleObjectManager<T extends BaseModel> extends BaseObjec
@Override
public final Set<Long> getUserItems(long userId) {
- if (!userItems.containsKey(userId)) {
- userItems.put(userId, new HashSet<Long>());
+ try {
+ readLock();
+ Set<Long> result = userItems.get(userId);
+ if (result != null) {
+ return new HashSet<>(result);
+ } else {
+ return new HashSet<>();
+ }
+ } finally {
+ readUnlock();
}
- return userItems.get(userId);
}
@Override
public Set<Long> getManagedItems(long userId) {
- Set<Long> result = new HashSet<>();
- result.addAll(getUserItems(userId));
+ Set<Long> result = getUserItems(userId);
for (long managedUserId : Context.getUsersManager().getUserItems(userId)) {
result.addAll(getUserItems(managedUserId));
}
@@ -71,16 +77,16 @@ public abstract class SimpleObjectManager<T extends BaseModel> extends BaseObjec
public final void refreshUserItems() {
if (getDataManager() != null) {
try {
- if (userItems != null) {
- userItems.clear();
- } else {
- userItems = new ConcurrentHashMap<>();
- }
+ writeLock();
+ userItems = new ConcurrentHashMap<>();
for (Permission permission : getDataManager().getPermissions(User.class, getBaseClass())) {
- getUserItems(permission.getOwnerId()).add(permission.getPropertyId());
+ Set<Long> items = userItems.computeIfAbsent(permission.getOwnerId(), key -> new HashSet<>());
+ items.add(permission.getPropertyId());
}
} catch (SQLException | ClassNotFoundException error) {
LOGGER.warn("Error getting permissions", error);
+ } finally {
+ writeUnlock();
}
}
}