You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by da...@apache.org on 2023/12/11 10:37:36 UTC

(causeway) 18/25: CAUSEWAY-3654: towards removing onOpen in TransactionServiceSpring

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

danhaywood pushed a commit to branch CAUSEWAY-3654-isis
in repository https://gitbox.apache.org/repos/asf/causeway.git

commit 2e75a2b38806a2ceb5890259d1e97225ab1a72b8
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Sun Dec 10 10:33:08 2023 +0000

    CAUSEWAY-3654: towards removing onOpen in TransactionServiceSpring
---
 .../transaction/TransactionServiceSpring.java      | 63 +++++++++++-----------
 1 file changed, 33 insertions(+), 30 deletions(-)

diff --git a/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/transaction/TransactionServiceSpring.java b/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/transaction/TransactionServiceSpring.java
index dec421a386..1bfc7154f6 100644
--- a/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/transaction/TransactionServiceSpring.java
+++ b/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/transaction/TransactionServiceSpring.java
@@ -42,6 +42,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.PlatformTransactionManager;
 import org.springframework.transaction.TransactionDefinition;
 import org.springframework.transaction.TransactionStatus;
+import org.springframework.transaction.support.DefaultTransactionDefinition;
 import org.springframework.transaction.support.TransactionSynchronizationManager;
 import org.springframework.transaction.support.TransactionTemplate;
 
@@ -115,7 +116,7 @@ implements
 
         try {
 
-            val tx = txManager.getTransaction(def);
+            TransactionStatus tx = txManager.getTransaction(def);
 
             result = Try.call(callable)
                     .mapFailure(ex->translateExceptionIfPossible(ex, txManager));
@@ -248,6 +249,12 @@ implements
     private ThreadLocal<LongAdder> txCounter = ThreadLocal.withInitial(LongAdder::new);
 
     /** TRANSACTION END BOUNDARY */
+
+    /**
+     * This is called by TransactionEventEmitter, which is a TransactionBoundaryAware bean.
+     *
+     * @param event
+     */
     @EventListener(TransactionAfterCompletionEvent.class)
     public void onTransactionEnded(final TransactionAfterCompletionEvent event) {
         txCounter.get().increment();
@@ -336,42 +343,38 @@ implements
             val onCloseTasks = _Lists.<CloseTask>newArrayList(platformTransactionManagers.size());
             interaction.putAttribute(OnCloseHandle.class, new OnCloseHandle(onCloseTasks));
 
-            platformTransactionManagers.forEach(txManager->newTransactionOrParticipateInExisting(txManager, onCloseTasks::add));
-        }
+            platformTransactionManagers.forEach(txManager -> {
 
-        transactionBoundaryAwareBeans.forEach(TransactionBoundaryAware::afterEnteringTransactionalBoundary);
-    }
+                val def = new DefaultTransactionDefinition();
+                def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
 
-    private void newTransactionOrParticipateInExisting(
-            final PlatformTransactionManager txManager,
-            final Consumer<CloseTask> onNewCloseTask) {
+                // either participate in existing or create new transaction
+                TransactionStatus txStatus = txManager.getTransaction(def);
+                if(txStatus==null // in support of JUnit testing (TransactionManagers might be mocked or hollow stubs)
+                        || !txStatus.isNewTransaction()) {
+                    // we are participating in an exiting transaction (or testing), nothing to do
+                    return;
+                }
 
-        val txTemplate = new TransactionTemplate(txManager);
-        txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
-
-        // either participate in existing or create new transaction
-        val txStatus = txManager.getTransaction(txTemplate);
-        if(txStatus==null // in support of JUnit testing (TransactionManagers might be mocked or hollow stubs)
-                || !txStatus.isNewTransaction()) {
-            // we are participating in an exiting transaction (or testing), nothing to do
-            return;
-        }
+                // we have created a new transaction, so need to provide a CloseTask
 
-        // we have created a new transaction, so need to provide a CloseTask
+                ((Consumer<CloseTask>) onCloseTasks::add).accept(
+                        new CloseTask(
+                                txStatus,
+                                txManager.getClass().getName(), // info to be used for display in case of errors
+                                ()->{
 
-        onNewCloseTask.accept(
-                new CloseTask(
-                        txStatus,
-                        txManager.getClass().getName(), // info to be used for display in case of errors
-                        ()->{
+                                    if(txStatus.isRollbackOnly()) {
+                                        txManager.rollback(txStatus);
+                                    } else {
+                                        txManager.commit(txStatus);
+                                    }
 
-                            if(txStatus.isRollbackOnly()) {
-                                txManager.rollback(txStatus);
-                            } else {
-                                txManager.commit(txStatus);
-                            }
+                                }));
+            });
+        }
 
-                        }));
+        transactionBoundaryAwareBeans.forEach(TransactionBoundaryAware::afterEnteringTransactionalBoundary);
     }