From c7bb65818e54b1587b92abc0ec208ddaa5b0f0ff Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Fri, 7 Feb 2020 09:34:02 +0100 Subject: [PATCH] Adding user to their own contacts, reusing one database transaction Fixes #15 --- src/main/java/envoy/server/Startup.java | 4 +++ .../server/database/PersistenceManager.java | 35 +++++++++---------- .../server/net/ObjectMessageProcessor.java | 1 + .../processors/LoginCredentialProcessor.java | 1 + 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index 82d9915..ede8d5c 100644 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -44,6 +44,10 @@ public class Startup { server.start(); server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance()); + + System.out.println("Press any key to stop the server..."); + System.in.read(); + System.exit(0); } private static void initializeCurrentMessageId() { diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index 803f177..84587e6 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -3,6 +3,7 @@ package envoy.server.database; import java.util.List; import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import envoy.server.data.ConfigItem; @@ -19,6 +20,9 @@ import envoy.server.data.User; */ public class PersistenceManager { + private final EntityManager entityManager = Persistence.createEntityManagerFactory("envoy").createEntityManager(); + private final EntityTransaction transaction = entityManager.getTransaction(); + private static final PersistenceManager persistenceManager = new PersistenceManager(); /** @@ -26,7 +30,10 @@ public class PersistenceManager { * * @since Envoy Server Standalone v0.1-alpha */ - private PersistenceManager() {} + private PersistenceManager() { + transaction.begin(); + Runtime.getRuntime().addShutdownHook(new Thread(() -> transaction.commit())); + } /** * @return the {@link PersistenceManager} singleton @@ -34,8 +41,6 @@ public class PersistenceManager { */ public static PersistenceManager getPersistenceManager() { return persistenceManager; } - private EntityManager entityManager = Persistence.createEntityManagerFactory("envoy").createEntityManager(); - /** * Adds a {@link User} to the database. * @@ -43,9 +48,8 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public void addUser(User User) { - entityManager.getTransaction().begin(); entityManager.persist(User); - entityManager.getTransaction().commit(); + entityManager.flush(); } /** @@ -55,9 +59,8 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public void addMessage(Message message) { - entityManager.getTransaction().begin(); entityManager.persist(message); - entityManager.getTransaction().commit(); + entityManager.flush(); } /** @@ -67,9 +70,8 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public void addConfigItem(ConfigItem configItem) { - entityManager.getTransaction().begin(); entityManager.persist(configItem); - entityManager.getTransaction().commit(); + entityManager.flush(); } /** @@ -79,9 +81,8 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public void updateUser(User user) { - entityManager.getTransaction().begin(); entityManager.merge(user); - entityManager.getTransaction().commit(); + entityManager.flush(); } /** @@ -91,9 +92,8 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public void updateMessage(Message message) { - entityManager.getTransaction().begin(); entityManager.merge(message); - entityManager.getTransaction().commit(); + entityManager.flush(); } /** @@ -103,9 +103,8 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public void updateConfigItem(ConfigItem configItem) { - entityManager.getTransaction().begin(); entityManager.merge(configItem); - entityManager.getTransaction().commit(); + entityManager.flush(); } /** @@ -115,9 +114,8 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public void deleteUser(User user) { - entityManager.getTransaction().begin(); entityManager.remove(user); - entityManager.getTransaction().commit(); + entityManager.flush(); } /** @@ -127,9 +125,8 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public void deleteMessage(Message message) { - entityManager.getTransaction().begin(); entityManager.remove(message); - entityManager.getTransaction().commit(); + entityManager.flush(); } /** diff --git a/src/main/java/envoy/server/net/ObjectMessageProcessor.java b/src/main/java/envoy/server/net/ObjectMessageProcessor.java index 3bbe43e..226fb69 100644 --- a/src/main/java/envoy/server/net/ObjectMessageProcessor.java +++ b/src/main/java/envoy/server/net/ObjectMessageProcessor.java @@ -33,6 +33,7 @@ public class ObjectMessageProcessor implements IMessageProcessor { */ public ObjectMessageProcessor(Set> processors) { this.processors = processors; } + @SuppressWarnings("unchecked") @Override public void process(Message message, WriteProxy writeProxy) { try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) { diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index acd2b2e..4a7f2c8 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -54,6 +54,7 @@ public class LoginCredentialProcessor implements ObjectProcessor