You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/08/12 23:55:31 UTC

[5/9] camel git commit: surrounded with try/catch/finally to ensure entity manager always closes

surrounded with try/catch/finally to ensure entity manager always closes


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e620a249
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e620a249
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e620a249

Branch: refs/heads/camel-2.19.x
Commit: e620a2496edc8d75e4e2bb7a650f95d293cb07bb
Parents: 18f7de6
Author: mkcochran <mc...@redhat.com>
Authored: Fri Aug 11 11:30:49 2017 -0400
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Aug 13 01:54:37 2017 +0200

----------------------------------------------------------------------
 .../idempotent/jpa/JpaMessageIdRepository.java  | 103 +++++++++++++------
 1 file changed, 70 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e620a249/components/camel-jpa/src/main/java/org/apache/camel/processor/idempotent/jpa/JpaMessageIdRepository.java
----------------------------------------------------------------------
diff --git a/components/camel-jpa/src/main/java/org/apache/camel/processor/idempotent/jpa/JpaMessageIdRepository.java b/components/camel-jpa/src/main/java/org/apache/camel/processor/idempotent/jpa/JpaMessageIdRepository.java
index 1d9b717..fabef9f 100644
--- a/components/camel-jpa/src/main/java/org/apache/camel/processor/idempotent/jpa/JpaMessageIdRepository.java
+++ b/components/camel-jpa/src/main/java/org/apache/camel/processor/idempotent/jpa/JpaMessageIdRepository.java
@@ -23,6 +23,7 @@ import java.util.List;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
+import javax.persistence.PersistenceException;
 import javax.persistence.Query;
 
 import org.apache.camel.Exchange;
@@ -88,7 +89,6 @@ public class JpaMessageIdRepository extends ServiceSupport implements ExchangeId
     @Override
     public boolean add(final Exchange exchange, final String messageId) {
         final EntityManager entityManager = getTargetEntityManager(exchange, entityManagerFactory, true, sharedEntityManager, true);
-
         // Run this in single transaction.
         Boolean rc = transactionTemplate.execute(new TransactionCallback<Boolean>() {
             public Boolean doInTransaction(TransactionStatus status) {
@@ -96,18 +96,31 @@ public class JpaMessageIdRepository extends ServiceSupport implements ExchangeId
                     entityManager.joinTransaction();
                 }
 
-                List<?> list = query(entityManager, messageId);
-                if (list.isEmpty()) {
-                    MessageProcessed processed = new MessageProcessed();
-                    processed.setProcessorName(processorName);
-                    processed.setMessageId(messageId);
-                    processed.setCreatedAt(new Date());
-                    entityManager.persist(processed);
-                    entityManager.flush();
-                    entityManager.close();
-                    return Boolean.TRUE;
-                } else {
-                    return Boolean.FALSE;
+                try {
+                	List<?> list = query(entityManager, messageId);
+                	if (list.isEmpty()) {
+                		MessageProcessed processed = new MessageProcessed();
+                		processed.setProcessorName(processorName);
+                		processed.setMessageId(messageId);
+                		processed.setCreatedAt(new Date());
+                		entityManager.persist(processed);
+                		entityManager.flush();
+                		entityManager.close();
+                		return Boolean.TRUE;
+                	} else {
+                		return Boolean.FALSE;
+                	}
+                } catch(Exception ex) {
+                	LOG.error("Something went wrong trying to add message to repository {}", ex);
+                	throw new PersistenceException(ex);
+                } finally {
+                    try {
+                        if (entityManager.isOpen()) {
+                            entityManager.close();
+                        }
+                    } catch (Exception e) {
+                        // ignore
+                    }
                 }
             }
         });
@@ -159,16 +172,28 @@ public class JpaMessageIdRepository extends ServiceSupport implements ExchangeId
                 if (isJoinTransaction()) {
                     entityManager.joinTransaction();
                 }
-
-                List<?> list = query(entityManager, messageId);
-                if (list.isEmpty()) {
-                    return Boolean.FALSE;
-                } else {
-                    MessageProcessed processed = (MessageProcessed) list.get(0);
-                    entityManager.remove(processed);
-                    entityManager.flush();
-                    entityManager.close();
-                    return Boolean.TRUE;
+                try{
+                	List<?> list = query(entityManager, messageId);
+                	if (list.isEmpty()) {
+                		return Boolean.FALSE;
+                	} else {
+                		MessageProcessed processed = (MessageProcessed) list.get(0);
+                		entityManager.remove(processed);
+                		entityManager.flush();
+                		entityManager.close();
+                		return Boolean.TRUE;
+                	}
+                } catch(Exception ex){
+                	LOG.error("Something went wrong trying to remove message to repository {}", ex);
+                	throw new PersistenceException(ex);
+                } finally {
+                	try {
+                		if (entityManager.isOpen()) {
+                			entityManager.close();
+                		}
+                	} catch (Exception e) {
+                		// ignore
+                	}
                 }
             }
         });
@@ -197,18 +222,30 @@ public class JpaMessageIdRepository extends ServiceSupport implements ExchangeId
                 if (isJoinTransaction()) {
                     entityManager.joinTransaction();
                 }
-
-                List<?> list = queryClear(entityManager);
-                if (!list.isEmpty()) {
-                    Iterator it = list.iterator();
-                    while (it.hasNext()) {
-                        Object item = it.next();
-                        entityManager.remove(item);
+                try {
+                	List<?> list = queryClear(entityManager);
+                	if (!list.isEmpty()) {
+                		Iterator it = list.iterator();
+                		while (it.hasNext()) {
+                			Object item = it.next();
+                			entityManager.remove(item);
+                		}
+                		entityManager.flush();
+                		entityManager.close();
+                	}
+                	return Boolean.TRUE;
+                } catch(Exception ex) {
+                	LOG.error("Something went wrong trying to clear the repository {}", ex);
+                	throw new PersistenceException(ex);
+                } finally {
+                    try {
+                        if (entityManager.isOpen()) {
+                            entityManager.close();
+                        }
+                    } catch (Exception e) {
+                        // ignore
                     }
-                    entityManager.flush();
-                    entityManager.close();
                 }
-                return Boolean.TRUE;
             }
         });