From 858662ba17097c0d9838facfc9e43c41d70a4104 Mon Sep 17 00:00:00 2001 From: delvh Date: Tue, 6 Oct 2020 12:14:18 +0200 Subject: [PATCH] Made PersistenceManager less error prone (and reduced amount of code) --- .../envoy/server/data/PersistenceManager.java | 63 +++++++------------ 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/server/src/main/java/envoy/server/data/PersistenceManager.java b/server/src/main/java/envoy/server/data/PersistenceManager.java index 67fa4e9..230dae8 100755 --- a/server/src/main/java/envoy/server/data/PersistenceManager.java +++ b/server/src/main/java/envoy/server/data/PersistenceManager.java @@ -28,15 +28,13 @@ public final class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ private PersistenceManager() { - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - transaction.begin(); + Runtime.getRuntime().addShutdownHook(new Thread(() -> transaction(() -> { ConnectionManager.getInstance() .getOnlineUsers() .stream() .map(this::getUserByID) .forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(Instant.now()); entityManager.merge(user); }); - transaction.commit(); - })); + }))); } /** @@ -239,10 +237,7 @@ public final class PersistenceManager { c2.getContacts().add(c1); // Synchronize changes with the database - transaction.begin(); - entityManager.merge(c1); - entityManager.merge(c2); - transaction.commit(); + transaction(() -> { entityManager.merge(c1); entityManager.merge(c2); }); } /** @@ -254,47 +249,33 @@ public final class PersistenceManager { return entityManager.createNamedQuery(User.findContacts).setParameter("user", user).getResultList(); } - private void persist(Object obj) { + private void persist(Object obj) { transaction(() -> entityManager.persist(obj)); } + + private void merge(Object obj) { transaction(() -> entityManager.merge(obj)); } + + private void remove(Object obj) { transaction(() -> entityManager.remove(obj)); } + + /** + * Performs a transaction with the given Runnable, that should somewhere call + * {@link EntityManager}. + * + * @param entityManagerRelatedAction the action that changes something in the + * database + * @since Envoy Server v0.3-beta + */ + private void transaction(Runnable entityManagerRelatedAction) { try { transaction.begin(); - entityManager.persist(obj); + entityManagerRelatedAction.run(); transaction.commit(); // Last transaction threw an error resulting in the transaction not being closed } catch (final IllegalStateException e) { if (transaction.isActive()) { transaction.rollback(); - persist(obj); - } - } - } - - private void merge(Object obj) { - try { - transaction.begin(); - entityManager.merge(obj); - transaction.commit(); - - // Last transaction threw an error resulting in the transaction not being closed - } catch (final IllegalStateException e) { - if (transaction.isActive()) { - transaction.rollback(); - merge(obj); - } - } - } - - private void remove(Object obj) { - try { - transaction.begin(); - entityManager.remove(obj); - transaction.commit(); - - // Last transaction threw an error resulting in the transaction not being closed - } catch (final IllegalStateException e) { - if (transaction.isActive()) { - transaction.rollback(); - remove(obj); + transaction.begin(); + entityManagerRelatedAction.run(); + transaction.commit(); } } } -- 2.45.2