This is an automated email from the ASF dual-hosted git repository.

zhfeng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 6d566472a81 Rewrite JdbcOrphanLockAwareIdempotentRepository::insert to 
not rely on exception (#9286)
6d566472a81 is described below

commit 6d566472a8138218ce5f74ad2e902fe08ab8312d
Author: Marco Bungart <[email protected]>
AuthorDate: Fri Feb 10 15:25:21 2023 +0100

    Rewrite JdbcOrphanLockAwareIdempotentRepository::insert to not rely on 
exception (#9286)
    
    * Removed the transaction since:
    - the initial exception will mark the transaction as dirty, blocking 
subsequent changes
    - not all databases support rollback of DDL (e.g MySQL, older Oracle DBs)
    
    * rewrote insert such that no exception is thrown
    
    * reverted changes to JdbcMessageIdRepository
    
    * format fixes
---
 .../jdbc/JdbcOrphanLockAwareIdempotentRepository.java    | 16 +++++++++-------
 .../idempotent/jdbc/JdbcMessageIdRepositoryTest.java     |  3 +--
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git 
a/components/camel-sql/src/main/java/org/apache/camel/processor/idempotent/jdbc/JdbcOrphanLockAwareIdempotentRepository.java
 
b/components/camel-sql/src/main/java/org/apache/camel/processor/idempotent/jdbc/JdbcOrphanLockAwareIdempotentRepository.java
index a84cbff205f..2934a71e153 100644
--- 
a/components/camel-sql/src/main/java/org/apache/camel/processor/idempotent/jdbc/JdbcOrphanLockAwareIdempotentRepository.java
+++ 
b/components/camel-sql/src/main/java/org/apache/camel/processor/idempotent/jdbc/JdbcOrphanLockAwareIdempotentRepository.java
@@ -31,7 +31,6 @@ import javax.sql.DataSource;
 import org.apache.camel.CamelContext;
 import org.apache.camel.ShutdownableService;
 import org.apache.camel.spi.ExecutorServiceManager;
-import org.springframework.dao.DuplicateKeyException;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.transaction.support.TransactionTemplate;
 
@@ -119,12 +118,15 @@ public class JdbcOrphanLockAwareIdempotentRepository 
extends JdbcMessageIdReposi
         Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
         long stamp = sl.writeLock();
         try {
-            int result = jdbcTemplate.update(getInsertString(), processorName, 
key, currentTimestamp);
-            processorNameMessageIdSet.add(new 
ProcessorNameAndMessageId(processorName, key));
-            return result;
-        } catch (DuplicateKeyException e) {
-            //Update in case of orphan lock where a process dies without 
releasing exist lock
-            return jdbcTemplate.update(getUpdateTimestampQuery(), 
currentTimestamp, processorName, key);
+            if (jdbcTemplate.queryForObject(getQueryString(), Integer.class, 
processorName, key) == 0) {
+                int result = jdbcTemplate.update(getInsertString(), 
processorName, key, currentTimestamp);
+                processorNameMessageIdSet.add(new 
ProcessorNameAndMessageId(processorName, key));
+                return result;
+            } else {
+                //Update in case of orphan lock where a process dies without 
releasing exist lock
+                return jdbcTemplate.update(getUpdateTimestampQuery(), 
currentTimestamp,
+                        processorName, key);
+            }
         } finally {
             sl.unlockWrite(stamp);
         }
diff --git 
a/components/camel-sql/src/test/java/org/apache/camel/processor/idempotent/jdbc/JdbcMessageIdRepositoryTest.java
 
b/components/camel-sql/src/test/java/org/apache/camel/processor/idempotent/jdbc/JdbcMessageIdRepositoryTest.java
index 904724d411d..5d11a8bff2c 100644
--- 
a/components/camel-sql/src/test/java/org/apache/camel/processor/idempotent/jdbc/JdbcMessageIdRepositoryTest.java
+++ 
b/components/camel-sql/src/test/java/org/apache/camel/processor/idempotent/jdbc/JdbcMessageIdRepositoryTest.java
@@ -45,7 +45,6 @@ public class JdbcMessageIdRepositoryTest extends 
CamelSpringTestSupport {
     protected static final String PROCESSOR_NAME = "myProcessorName";
 
     protected JdbcTemplate jdbcTemplate;
-    protected DataSource dataSource;
 
     @EndpointInject("mock:result")
     protected MockEndpoint resultEndpoint;
@@ -58,7 +57,7 @@ public class JdbcMessageIdRepositoryTest extends 
CamelSpringTestSupport {
     public void setUp() throws Exception {
         super.setUp();
 
-        dataSource = context.getRegistry().lookupByNameAndType("dataSource", 
DataSource.class);
+        DataSource dataSource = 
context.getRegistry().lookupByNameAndType("dataSource", DataSource.class);
         jdbcTemplate = new JdbcTemplate(dataSource);
         jdbcTemplate.afterPropertiesSet();
     }

Reply via email to