aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From timothyjw...@apache.org
Subject svn commit: r1740209 - in /aries/trunk/tx-control: tx-control-api/src/main/java/org/osgi/service/transaction/control/ tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/ tx-control-service-local/src/main/java/org/ap...
Date Wed, 20 Apr 2016 23:08:55 GMT
Author: timothyjward
Date: Wed Apr 20 23:08:55 2016
New Revision: 1740209

URL: http://svn.apache.org/viewvc?rev=1740209&view=rev
Log:
[tx-control] Add support for read-only transactions

Modified:
    aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionBuilder.java
    aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionContext.java
    aries/trunk/tx-control/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionControlImpl.java
    aries/trunk/tx-control/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextImpl.java
    aries/trunk/tx-control/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionContextImpl.java
    aries/trunk/tx-control/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionControlImpl.java
    aries/trunk/tx-control/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionContextTest.java
    aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextImpl.java
    aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlImpl.java
    aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextTest.java

Modified: aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionBuilder.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionBuilder.java?rev=1740209&r1=1740208&r2=1740209&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionBuilder.java
(original)
+++ aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionBuilder.java
Wed Apr 20 23:08:55 2016
@@ -131,4 +131,23 @@ public abstract class TransactionBuilder
 		noRollbackFor.addAll(Arrays.asList(throwables));
 		return this;
 	}
+	
+	/**
+	 * Indicate to the Transaction Control service that this transaction
+	 * will be read-only. This hint may be used by the Transaction Control
+	 * service and associated resources to optimise the transaction.
+	 * 
+	 * <p>
+	 * Note that this method is for optimisation purposes only. The TransactionControl
+	 * service is free to ignore the call if it does not offer read-only optimisation. 
+	 * 
+	 * <p>
+	 * If a transaction is marked read-only and then the scoped work performs a write
+	 * operation on a resource then this is a programming error. The resource is
+	 * free to raise an exception when the write is attempted, or to permit the write 
+	 * operation. As a result the transaction may commit successfully, or may rollback.
+	 * 
+	 * @return this builder
+	 */
+	public abstract TransactionBuilder readOnly();
 }

Modified: aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionContext.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionContext.java?rev=1740209&r1=1740208&r2=1740209&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionContext.java
(original)
+++ aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionContext.java
Wed Apr 20 23:08:55 2016
@@ -99,6 +99,14 @@ public interface TransactionContext {
 	boolean supportsLocal();
 
 	/**
+	 * @return true if the TransactionContext supports read-only optimisations
+	 * <em>and</em> the transaction was marked read only. In particular it is
+	 * legal for this method to return false even if the transaction was marked
+	 * read only by the initiating client.
+	 */
+	boolean isReadOnly();
+
+	/**
 	 * Register an XA resource with the current transaction
 	 * 
 	 * @param resource

Modified: aries/trunk/tx-control/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionControlImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionControlImpl.java?rev=1740209&r1=1740208&r2=1740209&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionControlImpl.java
(original)
+++ aries/trunk/tx-control/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/AbstractTransactionControlImpl.java
Wed Apr 20 23:08:55 2016
@@ -24,6 +24,14 @@ public abstract class AbstractTransactio
 
 	private final class TransactionBuilderImpl extends TransactionBuilder {
 
+		private boolean readOnly = false;
+		
+		@Override
+		public TransactionBuilder readOnly() {
+			readOnly = true;
+			return this;
+		}
+
 		private void checkExceptions() {
 			List<Class<? extends Throwable>> duplicates = rollbackFor.stream()
 					.filter(noRollbackFor::contains)
@@ -56,10 +64,12 @@ public abstract class AbstractTransactio
 					currentCoord = coordinator.begin(
 							"Resource-Local-Transaction.REQUIRED", 30000);
 					endCoordination = true;
-					currentTran = startTransaction(currentCoord);
+					currentTran = startTransaction(currentCoord, readOnly);
 					endTransaction = true;
 					currentCoord.getVariables().put(TransactionContextKey.class,
 							currentTran);
+				} else if (currentTran.isReadOnly() && !readOnly){
+					throw new TransactionException("A read only transaction is currently active, and cannot
be upgraded to a writeable transaction");
 				}
 			} catch (RuntimeException re) {
 				if(endTransaction) {
@@ -85,7 +95,7 @@ public abstract class AbstractTransactio
 				currentCoord = coordinator.begin(
 						"Resource-Local-Transaction.REQUIRES_NEW", 30000);
 
-				currentTran = startTransaction(currentCoord);
+				currentTran = startTransaction(currentCoord, readOnly);
 				currentCoord.getVariables().put(TransactionContextKey.class,
 						currentTran);
 			} catch (RuntimeException re) {
@@ -271,7 +281,7 @@ public abstract class AbstractTransactio
 		coordinator = c;
 	}
 
-	protected abstract AbstractTransactionContextImpl startTransaction(Coordination currentCoord);
+	protected abstract AbstractTransactionContextImpl startTransaction(Coordination currentCoord,
boolean readOnly);
 
 	@Override
 	public TransactionBuilder build() {

Modified: aries/trunk/tx-control/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextImpl.java?rev=1740209&r1=1740208&r2=1740209&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextImpl.java
(original)
+++ aries/trunk/tx-control/tx-control-service-common/src/main/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextImpl.java
Wed Apr 20 23:08:55 2016
@@ -83,6 +83,11 @@ public class NoTransactionContextImpl ex
 	}
 
 	@Override
+	public boolean isReadOnly() {
+		return false;
+	}
+
+	@Override
 	protected boolean isAlive() {
 		return !finished.get();
 	}

Modified: aries/trunk/tx-control/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionContextImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionContextImpl.java?rev=1740209&r1=1740208&r2=1740209&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionContextImpl.java
(original)
+++ aries/trunk/tx-control/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionContextImpl.java
Wed Apr 20 23:08:55 2016
@@ -24,10 +24,14 @@ public class TransactionContextImpl exte
 
 	final List<LocalResource> resources = new ArrayList<>();
 
+	private final boolean readOnly;
+
 	private AtomicReference<TransactionStatus> tranStatus = new AtomicReference<>(ACTIVE);
 
-	public TransactionContextImpl(Coordination coordination) {
+
+	public TransactionContextImpl(Coordination coordination, boolean readOnly) {
 		super(coordination);
+		this.readOnly = readOnly;
 	}
 
 	@Override
@@ -134,6 +138,11 @@ public class TransactionContextImpl exte
 	}
 
 	@Override
+	public boolean isReadOnly() {
+		return readOnly;
+	}
+
+	@Override
 	protected boolean isAlive() {
 		TransactionStatus status = tranStatus.get();
 		return status != COMMITTED && status != ROLLED_BACK;

Modified: aries/trunk/tx-control/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionControlImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionControlImpl.java?rev=1740209&r1=1740208&r2=1740209&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionControlImpl.java
(original)
+++ aries/trunk/tx-control/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionControlImpl.java
Wed Apr 20 23:08:55 2016
@@ -12,8 +12,8 @@ public class TransactionControlImpl exte
 	}
 
 	@Override
-	protected AbstractTransactionContextImpl startTransaction(Coordination currentCoord) {
-		return new TransactionContextImpl(currentCoord);
+	protected AbstractTransactionContextImpl startTransaction(Coordination currentCoord, boolean
readOnly) {
+		return new TransactionContextImpl(currentCoord, readOnly);
 	}
 	
 }

Modified: aries/trunk/tx-control/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionContextTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionContextTest.java?rev=1740209&r1=1740208&r2=1740209&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionContextTest.java
(original)
+++ aries/trunk/tx-control/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionContextTest.java
Wed Apr 20 23:08:55 2016
@@ -48,7 +48,7 @@ public class TransactionContextTest {
 	
 	@Before
 	public void setUp() {
-		ctx = new TransactionContextImpl(coordination);
+		ctx = new TransactionContextImpl(coordination, false);
 		variables = new HashMap<>();
 		Mockito.when(coordination.getVariables()).thenReturn(variables);
 	}
@@ -65,6 +65,17 @@ public class TransactionContextTest {
 	}
 
 	@Test
+	public void testisReadOnlyFalse() {
+		assertFalse(ctx.isReadOnly());
+	}
+
+	@Test
+	public void testisReadOnlyTrue() {
+		ctx = new TransactionContextImpl(coordination, true);
+		assertTrue(ctx.isReadOnly());
+	}
+
+	@Test
 	public void testTransactionKey() {
 		Mockito.when(coordination.getId()).thenReturn(42L);
 		

Modified: aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextImpl.java?rev=1740209&r1=1740208&r2=1740209&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextImpl.java
(original)
+++ aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextImpl.java
Wed Apr 20 23:08:55 2016
@@ -49,9 +49,13 @@ public class TransactionContextImpl exte
 	
 	private final Object key;
 
-	public TransactionContextImpl(GeronimoTransactionManager transactionManager, Coordination
coordination) {
+	private final boolean readOnly;
+
+	public TransactionContextImpl(GeronimoTransactionManager transactionManager, Coordination
coordination, 
+			boolean readOnly) {
 		super(coordination);
 		this.transactionManager = transactionManager;
+		this.readOnly = readOnly;
 		Transaction tmp = null;
 		try {
 			tmp = transactionManager.suspend();
@@ -222,6 +226,11 @@ public class TransactionContextImpl exte
 	}
 
 	@Override
+	public boolean isReadOnly() {
+		return readOnly;
+	}
+
+	@Override
 	protected boolean isAlive() {
 		TransactionStatus status = getTransactionStatus();
 		return status != COMMITTED && status != ROLLED_BACK;

Modified: aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlImpl.java?rev=1740209&r1=1740208&r2=1740209&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlImpl.java
(original)
+++ aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlImpl.java
Wed Apr 20 23:08:55 2016
@@ -16,8 +16,8 @@ public class TransactionControlImpl exte
 	}
 
 	@Override
-	protected AbstractTransactionContextImpl startTransaction(Coordination currentCoord) {
-		return new TransactionContextImpl(transactionManager, currentCoord);
+	protected AbstractTransactionContextImpl startTransaction(Coordination currentCoord, boolean
readOnly) {
+		return new TransactionContextImpl(transactionManager, currentCoord, readOnly);
 	}
 	
 }

Modified: aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextTest.java?rev=1740209&r1=1740208&r2=1740209&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextTest.java
(original)
+++ aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextTest.java
Wed Apr 20 23:08:55 2016
@@ -53,7 +53,7 @@ public class TransactionContextTest {
 	
 	@Before
 	public void setUp() throws XAException {
-		ctx = new TransactionContextImpl(new GeronimoTransactionManager(), coordination);
+		ctx = new TransactionContextImpl(new GeronimoTransactionManager(), coordination, false);
 		variables = new HashMap<>();
 		Mockito.when(coordination.getVariables()).thenReturn(variables);
 	}
@@ -68,6 +68,18 @@ public class TransactionContextTest {
 		ctx.setRollbackOnly();
 		assertTrue(ctx.getRollbackOnly());
 	}
+	
+	@Test
+	public void testisReadOnlyFalse() {
+		assertFalse(ctx.isReadOnly());
+	}
+
+	@Test
+	public void testisReadOnlyTrue() throws XAException {
+		ctx = new TransactionContextImpl(new GeronimoTransactionManager(), coordination, true);
+		assertTrue(ctx.isReadOnly());
+	}
+
 
 	@Test
 	public void testTransactionKey() {



Mime
View raw message