pefernan commented on code in PR #6850:
URL: 
https://github.com/apache/incubator-kie-drools/pull/6850#discussion_r3681715648


##########
kogito-data-index/data-index-storage/data-index-storage-jpa-common/src/main/java/org/kie/kogito/index/jpa/storage/ProcessDefinitionEntityStorage.java:
##########
@@ -44,4 +53,22 @@ public ProcessDefinitionEntityStorage(EntityManager em, 
Iterable<JsonPredicateBu
                 e.getVersion()), 
Optional.ofNullable(getInstance(predicateBuilder)), 
Optional.ofNullable(getInstance(processes)));
     }
 
+    protected ProcessDefinition put(ProcessDefinitionKey key, 
ProcessDefinition value,
+            Function<Supplier<ProcessDefinition>, ProcessDefinition> 
isolatedTransaction) {
+        try {
+            return isolatedTransaction.apply(() -> {
+                super.put(key, value);
+                em.flush();
+                return value;
+            });
+        } catch (RuntimeException e) {
+            if 
(Throwables.getCausalChain(e).stream().noneMatch(ConstraintViolationException.class::isInstance))
 {
+                throw e;
+            }
+            LOGGER.info("ProcessDefinition with id '{}' and version '{}' is 
already present, skipping insert.", key.getId(), key.getVersion());
+            LOGGER.debug("Duplicate ProcessDefinition insert suppressed", e);
+            return get(key);
+        }
+    }
+

Review Comment:
   I think this code could be simplified to something like:
   
   ```suggestion
       public ProcessDefinition put(ProcessDefinitionKey key, ProcessDefinition 
value) {
       try {
           return wrapInTransaction(() -> {
               super.put(key, value);
               em.flush();
               return value;
           });
       } catch (RuntimeException e) {
           if 
(Throwables.getCausalChain(e).stream().noneMatch(ConstraintViolationException.class::isInstance))
 {
               throw e;
           }
           LOGGER.info("ProcessDefinition with id '{}' and version '{}' is 
already present, skipping insert.", key.getId(), key.getVersion());
           LOGGER.debug("Duplicate ProcessDefinition insert suppressed", e);
           return wrapInTransaction(() -> get(key));
       }
   }
   
   protected abstract ProcessDefinition 
wrapInTransaction(Supplier<ProcessDefinition> supplier);
   
   ```



##########
kogito-data-index/data-index-storage/data-index-storage-jpa-common/src/main/java/org/kie/kogito/index/jpa/storage/ProcessDefinitionEntityStorage.java:
##########
@@ -44,4 +53,22 @@ public ProcessDefinitionEntityStorage(EntityManager em, 
Iterable<JsonPredicateBu
                 e.getVersion()), 
Optional.ofNullable(getInstance(predicateBuilder)), 
Optional.ofNullable(getInstance(processes)));
     }
 
+    protected ProcessDefinition put(ProcessDefinitionKey key, 
ProcessDefinition value,
+            Function<Supplier<ProcessDefinition>, ProcessDefinition> 
isolatedTransaction) {
+        try {
+            return isolatedTransaction.apply(() -> {
+                super.put(key, value);
+                em.flush();
+                return value;
+            });
+        } catch (RuntimeException e) {
+            if 
(Throwables.getCausalChain(e).stream().noneMatch(ConstraintViolationException.class::isInstance))
 {
+                throw e;
+            }
+            LOGGER.info("ProcessDefinition with id '{}' and version '{}' is 
already present, skipping insert.", key.getId(), key.getVersion());
+            LOGGER.debug("Duplicate ProcessDefinition insert suppressed", e);
+            return get(key);
+        }
+    }
+

Review Comment:
   Then each concrete implementation should only implement thet 
insertInNewTransaction method with the @Transactional annotation like:
   ```java
   @Override
   @Transactional(TxType.REQUIRES_NEW)
   protected ProcessDefinition wrapInTransaction(Supplier<ProcessDefinition> 
supplier) {
       return supplier.get();
   }
   
   // Spring Boot
   @Override
   @Transactional(Transactional.TxType.REQUIRES_NEW)
   protected ProcessDefinition wrapInTransaction(Supplier<ProcessDefinition> 
supplier) {
       return supplier.get();
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to