]> git.basschouten.com Git - openhab-addons.git/commitdiff
[jpa] Do not log failure to persist item with duplicate timestamp as error (#15978)
authorCody Cutrer <cody@cutrer.us>
Tue, 30 Jan 2024 21:37:19 +0000 (14:37 -0700)
committerGitHub <noreply@github.com>
Tue, 30 Jan 2024 21:37:19 +0000 (22:37 +0100)
* [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 <cody@cutrer.us>
bundles/org.openhab.persistence.jpa/src/main/java/org/openhab/persistence/jpa/internal/JpaPersistenceService.java

index 7cee97c04cd1c2c3d02cd12d862414176ba02daa..b782d4bd6d0cb5a13f0317f5f1356ae4ba35bd47 100644 (file)
@@ -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();