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/5b183fd4
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5b183fd4
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5b183fd4

Branch: refs/heads/camel-2.18.x
Commit: 5b183fd460fe62a63ca92e082bdcc63cd1274fa4
Parents: a1da011
Author: mkcochran <mcoch...@redhat.com>
Authored: Fri Aug 11 11:30:49 2017 -0400
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sun Aug 13 01:55:11 2017 +0200

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


http://git-wip-us.apache.org/repos/asf/camel/blob/5b183fd4/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;
             }
         });
 

Reply via email to