[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 <cody@cutrer.us>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Cody Cutrer 2024-01-30 14:37:19 -07:00 committed by Ciprian Pascu
parent ba3c98438d
commit 4208400baf

View 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) {
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();