diff --git a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcPersistenceService.java b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcPersistenceService.java index 6ba41ba03..73f3f29e5 100644 --- a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcPersistenceService.java +++ b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcPersistenceService.java @@ -21,10 +21,13 @@ import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import java.util.stream.Collectors; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.common.NamedThreadFactory; import org.openhab.core.config.core.ConfigurableService; import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.items.GroupItem; @@ -71,6 +74,9 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers private final ItemRegistry itemRegistry; + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, + new NamedThreadFactory(JdbcPersistenceServiceConstants.SERVICE_ID)); + @Activate public JdbcPersistenceService(final @Reference ItemRegistry itemRegistry, final @Reference TimeZoneProvider timeZoneProvider) { @@ -131,21 +137,21 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers @Override public void store(Item item) { - internalStore(item, null, item.getState()); + scheduler.execute(() -> internalStore(item, null, item.getState())); } @Override public void store(Item item, @Nullable String alias) { // alias is not supported - internalStore(item, null, item.getState()); + scheduler.execute(() -> internalStore(item, null, item.getState())); } @Override public void store(Item item, ZonedDateTime date, State state) { - internalStore(item, date, state); + scheduler.execute(() -> internalStore(item, date, state)); } - private void internalStore(Item item, @Nullable ZonedDateTime date, State state) { + private synchronized void internalStore(Item item, @Nullable ZonedDateTime date, State state) { // Do not store undefined/uninitialized data if (state instanceof UnDefType) { logger.debug("JDBC::store: ignore Item '{}' because it is UnDefType", item.getName());