aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/database/DriversManager.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/DriversManager.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/DriversManager.java')
-rw-r--r--src/main/java/org/traccar/database/DriversManager.java51
1 files changed, 39 insertions, 12 deletions
diff --git a/src/main/java/org/traccar/database/DriversManager.java b/src/main/java/org/traccar/database/DriversManager.java
index 930951460..d111cd643 100644
--- a/src/main/java/org/traccar/database/DriversManager.java
+++ b/src/main/java/org/traccar/database/DriversManager.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");
@@ -27,22 +27,44 @@ public class DriversManager extends ExtendedObjectManager<Driver> {
public DriversManager(DataManager dataManager) {
super(dataManager, Driver.class);
- if (driversByUniqueId == null) {
- driversByUniqueId = new ConcurrentHashMap<>();
+ try {
+ writeLock();
+ if (driversByUniqueId == null) {
+ driversByUniqueId = new ConcurrentHashMap<>();
+ }
+ } finally {
+ writeUnlock();
}
}
- private void putUniqueDriverId(Driver driver) {
- if (driversByUniqueId == null) {
- driversByUniqueId = new ConcurrentHashMap<>(getAllItems().size());
+ private void addByUniqueId(Driver driver) {
+ try {
+ writeLock();
+ if (driversByUniqueId == null) {
+ driversByUniqueId = new ConcurrentHashMap<>();
+ }
+ driversByUniqueId.put(driver.getUniqueId(), driver);
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ private void removeByUniqueId(String driverUniqueId) {
+ try {
+ writeLock();
+ if (driversByUniqueId == null) {
+ driversByUniqueId = new ConcurrentHashMap<>();
+ }
+ driversByUniqueId.remove(driverUniqueId);
+ } finally {
+ writeUnlock();
}
- driversByUniqueId.put(driver.getUniqueId(), driver);
}
@Override
protected void addNewItem(Driver driver) {
super.addNewItem(driver);
- putUniqueDriverId(driver);
+ addByUniqueId(driver);
}
@Override
@@ -50,9 +72,9 @@ public class DriversManager extends ExtendedObjectManager<Driver> {
Driver cachedDriver = getById(driver.getId());
cachedDriver.setName(driver.getName());
if (!driver.getUniqueId().equals(cachedDriver.getUniqueId())) {
- driversByUniqueId.remove(cachedDriver.getUniqueId());
+ removeByUniqueId(cachedDriver.getUniqueId());
cachedDriver.setUniqueId(driver.getUniqueId());
- putUniqueDriverId(cachedDriver);
+ addByUniqueId(cachedDriver);
}
cachedDriver.setAttributes(driver.getAttributes());
}
@@ -63,11 +85,16 @@ public class DriversManager extends ExtendedObjectManager<Driver> {
if (cachedDriver != null) {
String driverUniqueId = cachedDriver.getUniqueId();
super.removeCachedItem(driverId);
- driversByUniqueId.remove(driverUniqueId);
+ removeByUniqueId(driverUniqueId);
}
}
public Driver getDriverByUniqueId(String uniqueId) {
- return driversByUniqueId.get(uniqueId);
+ try {
+ readLock();
+ return driversByUniqueId.get(uniqueId);
+ } finally {
+ readUnlock();
+ }
}
}