You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ti...@apache.org on 2018/01/18 17:08:44 UTC

[aries-tx-control] 04/04: tx-control-service spec fixes

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

timothyjward pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-tx-control.git

commit bdae9a1357c05622abc4ea2298a1f636afbf2c00
Author: Tim Ward <ti...@apache.org>
AuthorDate: Thu Jan 18 17:08:29 2018 +0000

    tx-control-service spec fixes
    
    Users should be able to ignore specific exception instances before they are thrown
---
 .../impl/AbstractTransactionContextImpl.java       |  9 ++++++++
 .../impl/AbstractTransactionControlImpl.java       | 13 +++++------
 .../common/impl/NoTransactionContextTest.java      |  1 -
 .../local/impl/TransactionControlRunningTest.java  | 26 ++++++++++++++++++++++
 .../xa/impl/TransactionControlRunningTest.java     | 26 ++++++++++++++++++++++
 5 files changed, 67 insertions(+), 8 deletions(-)

diff --git a/tx-control-services/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionContextImpl.java b/tx-control-services/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionContextImpl.java
index 9c8337a..534a4f1 100644
--- a/tx-control-services/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionContextImpl.java
+++ b/tx-control-services/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionContextImpl.java
@@ -19,9 +19,12 @@
 package org.apache.aries.tx.control.service.common.impl;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.IdentityHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
 
@@ -38,6 +41,8 @@ public abstract class AbstractTransactionContextImpl implements TransactionConte
 
 	protected final List<Throwable> subsequentExceptions = new ArrayList<>();
 
+	protected final Set<Throwable> ignoredExceptions = Collections.newSetFromMap(new IdentityHashMap<>());
+
 	protected final List<Runnable> preCompletion = new ArrayList<>();
 
 	protected final List<Consumer<TransactionStatus>> postCompletion = new ArrayList<>();
@@ -91,4 +96,8 @@ public abstract class AbstractTransactionContextImpl implements TransactionConte
 	protected abstract void safeSetRollbackOnly();
 
 	public abstract void finish();
+
+	protected void ignoreException(Throwable t) {
+		ignoredExceptions.add(t);
+	}
 }
diff --git a/tx-control-services/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionControlImpl.java b/tx-control-services/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionControlImpl.java
index 24173dd..f37a06c 100644
--- a/tx-control-services/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionControlImpl.java
+++ b/tx-control-services/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionControlImpl.java
@@ -161,8 +161,8 @@ public abstract class AbstractTransactionControlImpl implements TransactionContr
 				result = transactionalWork.call();
 
 			} catch (Throwable t) {
-				//TODO handle noRollbackFor
-				if(requiresRollback(t)) {
+				if(!currentTran.ignoredExceptions.contains(t) && 
+						requiresRollback(t)) {
 					currentTran.safeSetRollbackOnly();
 				}
 				if(endTransaction) {
@@ -239,7 +239,7 @@ public abstract class AbstractTransactionControlImpl implements TransactionContr
 	private final ThreadLocal<AbstractTransactionContextImpl> existingTx = new ThreadLocal<>();
 	
 	private final AtomicBoolean closed = new AtomicBoolean();
-
+	
 	protected abstract AbstractTransactionContextImpl startTransaction(boolean readOnly);
 
 	@Override
@@ -291,8 +291,8 @@ public abstract class AbstractTransactionControlImpl implements TransactionContr
 		return getCurrentContext() != null;
 	}
 
-	private TransactionContext getCurrentTranContextChecked() {
-		TransactionContext toUse = getCurrentContext();
+	private AbstractTransactionContextImpl getCurrentTranContextChecked() {
+		AbstractTransactionContextImpl toUse = existingTx.get();
 		if (toUse == null) {
 			throw new IllegalStateException(
 					"There is no applicable transaction context");
@@ -307,8 +307,7 @@ public abstract class AbstractTransactionControlImpl implements TransactionContr
 
 	@Override
 	public void ignoreException(Throwable t) throws IllegalStateException {
-		// TODO Auto-generated method stub
-
+		getCurrentTranContextChecked().ignoreException(t);
 	}
 
 	public void close() {
diff --git a/tx-control-services/tx-control-service-common/src/test/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextTest.java b/tx-control-services/tx-control-service-common/src/test/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextTest.java
index d1e1219..bba856e 100644
--- a/tx-control-services/tx-control-service-common/src/test/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextTest.java
+++ b/tx-control-services/tx-control-service-common/src/test/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextTest.java
@@ -23,7 +23,6 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
-import static org.osgi.service.transaction.control.TransactionStatus.COMMITTED;
 import static org.osgi.service.transaction.control.TransactionStatus.NO_TRANSACTION;
 
 import java.util.concurrent.atomic.AtomicInteger;
diff --git a/tx-control-services/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionControlRunningTest.java b/tx-control-services/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionControlRunningTest.java
index 5564fa5..f43577d 100644
--- a/tx-control-services/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionControlRunningTest.java
+++ b/tx-control-services/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionControlRunningTest.java
@@ -140,6 +140,32 @@ public class TransactionControlRunningTest {
 	}
 
 	@Test
+	public void testRequiredIgnoreException() {
+		
+		AtomicReference<TransactionStatus> finalStatus = new AtomicReference<>();
+		
+		Exception userEx = new BindException("Bang!");
+		
+		try {
+			txControl.required(() -> {
+				
+				assertTrue(txControl.activeTransaction());
+				
+				txControl.getCurrentContext().postCompletion(finalStatus::set);
+				
+				txControl.ignoreException(userEx);
+				
+				throw userEx;
+			});
+			fail("Should not be reached");
+		} catch (ScopedWorkException swe) {
+			assertSame(userEx, swe.getCause());
+		}
+		
+		assertEquals(COMMITTED, finalStatus.get());
+	}
+
+	@Test
 	public void testTwoRequiredsNested() {
 
 		AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
diff --git a/tx-control-services/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlRunningTest.java b/tx-control-services/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlRunningTest.java
index dbfa3c7..a7ad5d0 100644
--- a/tx-control-services/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlRunningTest.java
+++ b/tx-control-services/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlRunningTest.java
@@ -141,6 +141,32 @@ public class TransactionControlRunningTest {
 	}
 
 	@Test
+	public void testRequiredIgnoreException() {
+		
+		AtomicReference<TransactionStatus> finalStatus = new AtomicReference<>();
+		
+		Exception userEx = new BindException("Bang!");
+		
+		try {
+			txControl.required(() -> {
+				
+				assertTrue(txControl.activeTransaction());
+				
+				txControl.getCurrentContext().postCompletion(finalStatus::set);
+				
+				txControl.ignoreException(userEx);
+				
+				throw userEx;
+			});
+			fail("Should not be reached");
+		} catch (ScopedWorkException swe) {
+			assertSame(userEx, swe.getCause());
+		}
+		
+		assertEquals(COMMITTED, finalStatus.get());
+	}
+
+	@Test
 	public void testTwoRequiredsNested() {
 
 		AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();

-- 
To stop receiving notification emails like this one, please contact
"commits@aries.apache.org" <co...@aries.apache.org>.