From: Cody Cutrer Date: Tue, 30 Jan 2024 21:37:19 +0000 (-0700) Subject: [jpa] Do not log failure to persist item with duplicate timestamp as error (#15978) X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=6bd0c288943a5e42f77f06fe4f0a2b694ca0206d;p=openhab-addons.git [jpa] Do not log failure to persist item with duplicate timestamp as error (#15978) * [jpa] ignore EntityExistsException in case the user manually added a UNIQUE constraint to the database, openHAB might send duplicate timestamps. effectively this means the first attempt is kept, while others are dropped. as long as you're using sub-second timestamps, this shouldn't be an issue - the state updates truly should be duplicates Signed-off-by: Cody Cutrer --- diff --git a/bundles/org.openhab.persistence.jpa/src/main/java/org/openhab/persistence/jpa/internal/JpaPersistenceService.java b/bundles/org.openhab.persistence.jpa/src/main/java/org/openhab/persistence/jpa/internal/JpaPersistenceService.java index 7cee97c04c..b782d4bd6d 100644 --- a/bundles/org.openhab.persistence.jpa/src/main/java/org/openhab/persistence/jpa/internal/JpaPersistenceService.java +++ b/bundles/org.openhab.persistence.jpa/src/main/java/org/openhab/persistence/jpa/internal/JpaPersistenceService.java @@ -19,6 +19,7 @@ import java.util.Locale; import java.util.Map; import java.util.Set; +import javax.persistence.EntityExistsException; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; @@ -164,7 +165,13 @@ public class JpaPersistenceService implements QueryablePersistenceService { em.getTransaction().commit(); logger.debug("Persisting item...done"); } catch (Exception e) { - logger.error("Error while persisting item! Rolling back!", e); + if (e.getCause() instanceof EntityExistsException) { + // there's a UNIQUE constraint in the database, and we tried to write + // a duplicate timestamp. Just ignore + logger.debug("Failed to persist item {} because of duplicate timestamp", name); + } else { + logger.error("Error while persisting item! Rolling back!", e); + } em.getTransaction().rollback(); } finally { em.close();