aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/session
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/session')
-rw-r--r--src/main/java/org/traccar/session/ConnectionManager.java7
-rw-r--r--src/main/java/org/traccar/session/cache/CacheManager.java19
2 files changed, 21 insertions, 5 deletions
diff --git a/src/main/java/org/traccar/session/ConnectionManager.java b/src/main/java/org/traccar/session/ConnectionManager.java
index 05c4893fd..fe6521d18 100644
--- a/src/main/java/org/traccar/session/ConnectionManager.java
+++ b/src/main/java/org/traccar/session/ConnectionManager.java
@@ -144,7 +144,10 @@ public class ConnectionManager {
endpointSessions.put(device.getUniqueId(), deviceSession);
sessionsByEndpoint.put(endpoint, endpointSessions);
sessionsByDeviceId.put(device.getId(), deviceSession);
- cacheManager.addDevice(device.getId());
+
+ if (oldSession == null) {
+ cacheManager.addDevice(device.getId());
+ }
return deviceSession;
} else {
@@ -190,8 +193,8 @@ public class ConnectionManager {
public void deviceUnknown(long deviceId) {
updateDevice(deviceId, Device.STATUS_UNKNOWN, null);
DeviceSession deviceSession = sessionsByDeviceId.remove(deviceId);
- cacheManager.removeDevice(deviceId);
if (deviceSession != null) {
+ cacheManager.removeDevice(deviceId);
Endpoint endpoint = new Endpoint(deviceSession.getChannel(), deviceSession.getRemoteAddress());
sessionsByEndpoint.computeIfPresent(endpoint, (e, sessions) -> {
sessions.remove(deviceSession.getUniqueId());
diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java
index 87384f746..abc8ca4c9 100644
--- a/src/main/java/org/traccar/session/cache/CacheManager.java
+++ b/src/main/java/org/traccar/session/cache/CacheManager.java
@@ -60,6 +60,7 @@ public class CacheManager {
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Map<CacheKey, CacheValue> deviceCache = new HashMap<>();
+ private final Map<Long, Integer> deviceReferences = new HashMap<>();
private final Map<Long, Map<Class<? extends BaseModel>, List<Long>>> deviceLinks = new HashMap<>();
private final Map<Long, Position> devicePositions = new HashMap<>();
@@ -136,9 +137,14 @@ public class CacheManager {
public void addDevice(long deviceId) throws StorageException {
try {
lock.writeLock().lock();
- if (!deviceLinks.containsKey(deviceId)) {
+ Integer references = deviceReferences.get(deviceId);
+ if (references != null) {
+ references += 1;
+ } else {
unsafeAddDevice(deviceId);
+ references = 1;
}
+ deviceReferences.put(deviceId, references);
} finally {
lock.writeLock().unlock();
}
@@ -147,8 +153,15 @@ public class CacheManager {
public void removeDevice(long deviceId) {
try {
lock.writeLock().lock();
- if (deviceLinks.containsKey(deviceId)) {
- unsafeRemoveDevice(deviceId);
+ Integer references = deviceReferences.get(deviceId);
+ if (references != null) {
+ references -= 1;
+ if (references <= 0) {
+ unsafeRemoveDevice(deviceId);
+ deviceReferences.remove(deviceId);
+ } else {
+ deviceReferences.put(deviceId, references);
+ }
}
} finally {
lock.writeLock().unlock();