Browse Source

Restore TransactionOperations Kotlin API compatibilty

This commit renames the Runnable variant to executeWithoutResult
and uses a Consumer<TransactionStatus> parameter for better
consistency with TransactionCallbackWithoutResult.

Closes gh-23724
pull/23728/head
Sebastien Deleuze 5 years ago
parent
commit
b24ac74106
  1. 8
      spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheAnnotationTests.java
  2. 12
      spring-context-support/src/test/java/org/springframework/cache/transaction/TransactionAwareCacheDecoratorTests.java
  3. 2
      spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java
  4. 8
      spring-tx/src/main/java/org/springframework/transaction/support/TransactionOperations.java
  5. 7
      spring-tx/src/main/java/org/springframework/transaction/support/WithoutTransactionOperations.java

8
spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheAnnotationTests.java vendored

@ -83,22 +83,22 @@ public class JCacheEhCacheAnnotationTests extends AbstractCacheAnnotationTests { @@ -83,22 +83,22 @@ public class JCacheEhCacheAnnotationTests extends AbstractCacheAnnotationTests {
@Test
public void testEvictWithTransaction() {
txTemplate.execute(() -> testEvict(this.cs, false));
txTemplate.executeWithoutResult(s -> testEvict(this.cs, false));
}
@Test
public void testEvictEarlyWithTransaction() {
txTemplate.execute(() -> testEvictEarly(this.cs));
txTemplate.executeWithoutResult(s -> testEvictEarly(this.cs));
}
@Test
public void testEvictAllWithTransaction() {
txTemplate.execute(() -> testEvictAll(this.cs, false));
txTemplate.executeWithoutResult(s -> testEvictAll(this.cs, false));
}
@Test
public void testEvictAllEarlyWithTransaction() {
txTemplate.execute(() -> testEvictAllEarly(this.cs));
txTemplate.executeWithoutResult(s -> testEvictAllEarly(this.cs));
}

12
spring-context-support/src/test/java/org/springframework/cache/transaction/TransactionAwareCacheDecoratorTests.java vendored

@ -79,7 +79,7 @@ public class TransactionAwareCacheDecoratorTests { @@ -79,7 +79,7 @@ public class TransactionAwareCacheDecoratorTests {
Cache cache = new TransactionAwareCacheDecorator(target);
Object key = new Object();
txTemplate.execute(() -> {
txTemplate.executeWithoutResult(s -> {
cache.put(key, "123");
assertThat(target.get(key)).isNull();
});
@ -106,7 +106,7 @@ public class TransactionAwareCacheDecoratorTests { @@ -106,7 +106,7 @@ public class TransactionAwareCacheDecoratorTests {
Cache cache = new TransactionAwareCacheDecorator(target);
Object key = new Object();
txTemplate.execute(() -> {
txTemplate.executeWithoutResult(s -> {
assertThat(cache.putIfAbsent(key, "123")).isNull();
assertThat(target.get(key, String.class)).isEqualTo("123");
assertThat(cache.putIfAbsent(key, "456").get()).isEqualTo("123");
@ -135,7 +135,7 @@ public class TransactionAwareCacheDecoratorTests { @@ -135,7 +135,7 @@ public class TransactionAwareCacheDecoratorTests {
Object key = new Object();
cache.put(key, "123");
txTemplate.execute(() -> {
txTemplate.executeWithoutResult(s -> {
cache.evict(key);
assertThat(target.get(key, String.class)).isEqualTo("123");
});
@ -161,7 +161,7 @@ public class TransactionAwareCacheDecoratorTests { @@ -161,7 +161,7 @@ public class TransactionAwareCacheDecoratorTests {
Object key = new Object();
cache.put(key, "123");
txTemplate.execute(() -> {
txTemplate.executeWithoutResult(s -> {
cache.evictIfPresent(key);
assertThat(target.get(key)).isNull();
});
@ -187,7 +187,7 @@ public class TransactionAwareCacheDecoratorTests { @@ -187,7 +187,7 @@ public class TransactionAwareCacheDecoratorTests {
Object key = new Object();
cache.put(key, "123");
txTemplate.execute(() -> {
txTemplate.executeWithoutResult(s -> {
cache.clear();
assertThat(target.get(key, String.class)).isEqualTo("123");
});
@ -213,7 +213,7 @@ public class TransactionAwareCacheDecoratorTests { @@ -213,7 +213,7 @@ public class TransactionAwareCacheDecoratorTests {
Object key = new Object();
cache.put(key, "123");
txTemplate.execute(() -> {
txTemplate.executeWithoutResult(s -> {
cache.invalidate();
assertThat(target.get(key)).isNull();
});

2
spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java

@ -260,7 +260,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen @@ -260,7 +260,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
TransactionDefinition.PROPAGATION_REQUIRED);
TransactionAttribute txAttr = TestContextTransactionUtils.createDelegatingTransactionAttribute(
testContext, new DefaultTransactionAttribute(propagation));
new TransactionTemplate(txMgr, txAttr).execute(() -> populator.execute(finalDataSource));
new TransactionTemplate(txMgr, txAttr).executeWithoutResult(s -> populator.execute(finalDataSource));
}
}

8
spring-tx/src/main/java/org/springframework/transaction/support/TransactionOperations.java

@ -16,8 +16,11 @@ @@ -16,8 +16,11 @@
package org.springframework.transaction.support;
import java.util.function.Consumer;
import org.springframework.lang.Nullable;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
/**
* Interface specifying basic transaction execution operations.
@ -40,6 +43,7 @@ public interface TransactionOperations { @@ -40,6 +43,7 @@ public interface TransactionOperations {
* @return a result object returned by the callback, or {@code null} if none
* @throws TransactionException in case of initialization, rollback, or system errors
* @throws RuntimeException if thrown by the TransactionCallback
* @see #executeWithoutResult(Consumer)
*/
@Nullable
<T> T execute(TransactionCallback<T> action) throws TransactionException;
@ -59,9 +63,9 @@ public interface TransactionOperations { @@ -59,9 +63,9 @@ public interface TransactionOperations {
* @see #execute(TransactionCallback)
* @see TransactionCallbackWithoutResult
*/
default void execute(Runnable action) throws TransactionException {
default void executeWithoutResult(Consumer<TransactionStatus> action) throws TransactionException {
execute(status -> {
action.run();
action.accept(status);
return null;
});
}

7
spring-tx/src/main/java/org/springframework/transaction/support/WithoutTransactionOperations.java

@ -16,8 +16,11 @@ @@ -16,8 +16,11 @@
package org.springframework.transaction.support;
import java.util.function.Consumer;
import org.springframework.lang.Nullable;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
/**
* A {@link TransactionOperations} implementation which executes a given
@ -43,8 +46,8 @@ final class WithoutTransactionOperations implements TransactionOperations { @@ -43,8 +46,8 @@ final class WithoutTransactionOperations implements TransactionOperations {
}
@Override
public void execute(Runnable action) throws TransactionException {
action.run();
public void executeWithoutResult(Consumer<TransactionStatus> action) throws TransactionException {
action.accept(new SimpleTransactionStatus(false));
}
}

Loading…
Cancel
Save