Fixed Transactions not Getting Closed on the Server #42
@@ -3,9 +3,7 @@ package envoy.server.data;
 | 
				
			|||||||
import java.time.Instant;
 | 
					import java.time.Instant;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import javax.persistence.EntityManager;
 | 
					import javax.persistence.*;
 | 
				
			||||||
import javax.persistence.EntityTransaction;
 | 
					 | 
				
			||||||
import javax.persistence.Persistence;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import envoy.data.User.UserStatus;
 | 
					import envoy.data.User.UserStatus;
 | 
				
			||||||
import envoy.server.net.ConnectionManager;
 | 
					import envoy.server.net.ConnectionManager;
 | 
				
			||||||
@@ -235,8 +233,8 @@ public final class PersistenceManager {
 | 
				
			|||||||
	public void addContactBidirectional(long contactID1, long contactID2) {
 | 
						public void addContactBidirectional(long contactID1, long contactID2) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Get users by ID
 | 
							// Get users by ID
 | 
				
			||||||
		Contact	c1	= getContactByID(contactID1);
 | 
							final var	c1	= getContactByID(contactID1);
 | 
				
			||||||
		Contact	c2	= getContactByID(contactID2);
 | 
							final var	c2	= getContactByID(contactID2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Add users to each others contact lists
 | 
							// Add users to each others contact lists
 | 
				
			||||||
		c1.getContacts().add(c2);
 | 
							c1.getContacts().add(c2);
 | 
				
			||||||
@@ -259,20 +257,47 @@ public final class PersistenceManager {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private void persist(Object obj) {
 | 
						private void persist(Object obj) {
 | 
				
			||||||
 | 
							try {
 | 
				
			||||||
			transaction.begin();
 | 
								transaction.begin();
 | 
				
			||||||
			entityManager.persist(obj);
 | 
								entityManager.persist(obj);
 | 
				
			||||||
			transaction.commit();
 | 
								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) {
 | 
						private void merge(Object obj) {
 | 
				
			||||||
 | 
							try {
 | 
				
			||||||
			transaction.begin();
 | 
								transaction.begin();
 | 
				
			||||||
			entityManager.merge(obj);
 | 
								entityManager.merge(obj);
 | 
				
			||||||
			transaction.commit();
 | 
								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) {
 | 
						private void remove(Object obj) {
 | 
				
			||||||
 | 
							try {
 | 
				
			||||||
			transaction.begin();
 | 
								transaction.begin();
 | 
				
			||||||
			entityManager.remove(obj);
 | 
								entityManager.remove(obj);
 | 
				
			||||||
			transaction.commit();
 | 
								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);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user