From 2b2d8313c8a5b973b0da341a2ff19f24567b3582 Mon Sep 17 00:00:00 2001 From: mkcochran Date: Thu, 10 Aug 2017 15:54:38 -0400 Subject: [PATCH 1/2] closing entity manager --- .../camel/processor/idempotent/jpa/JpaMessageIdRepository.java | 3 +++ 1 file changed, 3 insertions(+) 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 f992715622c6b..1d9b71754f974 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 @@ -104,6 +104,7 @@ public Boolean doInTransaction(TransactionStatus status) { processed.setCreatedAt(new Date()); entityManager.persist(processed); entityManager.flush(); + entityManager.close(); return Boolean.TRUE; } else { return Boolean.FALSE; @@ -166,6 +167,7 @@ public Boolean doInTransaction(TransactionStatus status) { MessageProcessed processed = (MessageProcessed) list.get(0); entityManager.remove(processed); entityManager.flush(); + entityManager.close(); return Boolean.TRUE; } } @@ -204,6 +206,7 @@ public Boolean doInTransaction(TransactionStatus status) { entityManager.remove(item); } entityManager.flush(); + entityManager.close(); } return Boolean.TRUE; } From 40e1fcaf274a94245e1d6a797e85d2582bd715a1 Mon Sep 17 00:00:00 2001 From: mkcochran Date: Fri, 11 Aug 2017 11:30:49 -0400 Subject: [PATCH 2/2] surrounded with try/catch/finally to ensure entity manager always closes --- .../jpa/JpaMessageIdRepository.java | 103 ++++++++++++------ 1 file changed, 70 insertions(+), 33 deletions(-) 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 1d9b71754f974..fabef9ff19b69 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 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 boolean add(String messageId) { @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() { public Boolean doInTransaction(TransactionStatus status) { @@ -96,18 +96,31 @@ public Boolean doInTransaction(TransactionStatus status) { 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 Boolean doInTransaction(TransactionStatus status) { 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 Boolean doInTransaction(TransactionStatus status) { 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; } });