Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id B1D55200CF5 for ; Sun, 13 Aug 2017 01:55:32 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id B07281649A6; Sat, 12 Aug 2017 23:55:32 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 989BB164996 for ; Sun, 13 Aug 2017 01:55:31 +0200 (CEST) Received: (qmail 4498 invoked by uid 500); 12 Aug 2017 23:55:30 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 4178 invoked by uid 99); 12 Aug 2017 23:55:30 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 12 Aug 2017 23:55:30 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id DFC05F3307; Sat, 12 Aug 2017 23:55:28 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Date: Sat, 12 Aug 2017 23:55:34 -0000 Message-Id: <3d3080d77c0240d1a470d96a19fd06bb@git.apache.org> In-Reply-To: <4d9d44fe5faf4e61b0e7ffba60d22a5d@git.apache.org> References: <4d9d44fe5faf4e61b0e7ffba60d22a5d@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [8/9] camel git commit: surrounded with try/catch/finally to ensure entity manager always closes archived-at: Sat, 12 Aug 2017 23:55:32 -0000 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 Authored: Fri Aug 11 11:30:49 2017 -0400 Committer: Claus Ibsen 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() { 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; } });