Merge pull request #21 from informatik-ag-ngl/f/single_transaction

Adding user to their own contacts, reusing one database transaction
This commit is contained in:
Kai S. K. Engelbart 2020-02-07 20:35:12 +01:00 committed by GitHub
commit 48e12dcdfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 19 deletions

View File

@ -44,6 +44,10 @@ public class Startup {
server.start();
server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance());
System.out.println("Press the return key to stop the server...");
System.in.read();
System.exit(0);
}
private static void initializeCurrentMessageId() {

View File

@ -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();
}
/**

View File

@ -33,6 +33,7 @@ public class ObjectMessageProcessor implements IMessageProcessor {
*/
public ObjectMessageProcessor(Set<ObjectProcessor<?>> 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))) {

View File

@ -54,6 +54,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
// Create contacts
Contacts contacts = new Contacts(user.getId(),
user.getContacts().stream().map(envoy.server.data.User::toCommonUser).collect(Collectors.toList()));
contacts.getContacts().add(user.toCommonUser());
// Complete handshake
System.out.println("Sending user...");