Skip to content

Commit

Permalink
Fix TransactionOperations API compat is Kotlin
Browse files Browse the repository at this point in the history
This commit introduces a Supplier variant to execute and renames the
Runnable variant to executeWithoutResult.

Closes spring-projectsgh-23724
  • Loading branch information
sdeleuze committed Sep 28, 2019
1 parent ae22187 commit b872fc5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
Expand Up @@ -83,22 +83,22 @@ public void testCustomCacheManager() {

@Test
public void testEvictWithTransaction() {
txTemplate.execute(() -> testEvict(this.cs, false));
txTemplate.executeWithoutResult(() -> testEvict(this.cs, false));
}

@Test
public void testEvictEarlyWithTransaction() {
txTemplate.execute(() -> testEvictEarly(this.cs));
txTemplate.executeWithoutResult(() -> testEvictEarly(this.cs));
}

@Test
public void testEvictAllWithTransaction() {
txTemplate.execute(() -> testEvictAll(this.cs, false));
txTemplate.executeWithoutResult(() -> testEvictAll(this.cs, false));
}

@Test
public void testEvictAllEarlyWithTransaction() {
txTemplate.execute(() -> testEvictAllEarly(this.cs));
txTemplate.executeWithoutResult(() -> testEvictAllEarly(this.cs));
}


Expand Down
Expand Up @@ -79,7 +79,7 @@ public void putTransactional() {
Cache cache = new TransactionAwareCacheDecorator(target);
Object key = new Object();

txTemplate.execute(() -> {
txTemplate.executeWithoutResult(() -> {
cache.put(key, "123");
assertThat(target.get(key)).isNull();
});
Expand All @@ -106,7 +106,7 @@ public void putIfAbsentTransactional() { // no transactional support for putIfA
Cache cache = new TransactionAwareCacheDecorator(target);
Object key = new Object();

txTemplate.execute(() -> {
txTemplate.executeWithoutResult(() -> {
assertThat(cache.putIfAbsent(key, "123")).isNull();
assertThat(target.get(key, String.class)).isEqualTo("123");
assertThat(cache.putIfAbsent(key, "456").get()).isEqualTo("123");
Expand Down Expand Up @@ -135,7 +135,7 @@ public void evictTransactional() {
Object key = new Object();
cache.put(key, "123");

txTemplate.execute(() -> {
txTemplate.executeWithoutResult(() -> {
cache.evict(key);
assertThat(target.get(key, String.class)).isEqualTo("123");
});
Expand All @@ -161,7 +161,7 @@ public void evictIfPresentTransactional() { // no transactional support for evi
Object key = new Object();
cache.put(key, "123");

txTemplate.execute(() -> {
txTemplate.executeWithoutResult(() -> {
cache.evictIfPresent(key);
assertThat(target.get(key)).isNull();
});
Expand All @@ -187,7 +187,7 @@ public void clearTransactional() {
Object key = new Object();
cache.put(key, "123");

txTemplate.execute(() -> {
txTemplate.executeWithoutResult(() -> {
cache.clear();
assertThat(target.get(key, String.class)).isEqualTo("123");
});
Expand All @@ -213,7 +213,7 @@ public void invalidateTransactional() { // no transactional support for invalid
Object key = new Object();
cache.put(key, "123");

txTemplate.execute(() -> {
txTemplate.executeWithoutResult(() -> {
cache.invalidate();
assertThat(target.get(key)).isNull();
});
Expand Down
Expand Up @@ -16,6 +16,8 @@

package org.springframework.transaction.support;

import java.util.function.Supplier;

import org.springframework.lang.Nullable;
import org.springframework.transaction.TransactionException;

Expand All @@ -40,10 +42,31 @@ 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 #execute(Supplier)
* @see #executeWithoutResult(Runnable)
*/
@Nullable
<T> T execute(TransactionCallback<T> action) throws TransactionException;

/**
* Execute the action specified by the given callback object within a transaction.
* <p>Allows for returning a result object created within the transaction, that is,
* a domain object or a collection of domain objects. A RuntimeException thrown
* by the callback is treated as a fatal exception that enforces a rollback.
* Such an exception gets propagated to the caller of the template.
* @param action the Supplier that specifies the transactional action
* @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
* @since 5.2
* @see #execute(TransactionCallback)
* @see #executeWithoutResult(Runnable)
*/
@Nullable
default <T> T execute(Supplier<T> action) throws TransactionException {
return execute(status -> action.get());
}

/**
* Execute the action specified by the given {@link Runnable} within a transaction.
* <p>If you need to return an object from the callback or access the
Expand All @@ -57,10 +80,11 @@ public interface TransactionOperations {
* @throws RuntimeException if thrown by the Runnable
* @since 5.2
* @see #execute(TransactionCallback)
* @see #execute(Supplier)
* @see TransactionCallbackWithoutResult
*/
default void execute(Runnable action) throws TransactionException {
execute(status -> {
default void executeWithoutResult(Runnable action) throws TransactionException {
execute(() -> {
action.run();
return null;
});
Expand Down
Expand Up @@ -16,6 +16,8 @@

package org.springframework.transaction.support;

import java.util.function.Supplier;

import org.springframework.lang.Nullable;
import org.springframework.transaction.TransactionException;

Expand Down Expand Up @@ -43,7 +45,12 @@ public <T> T execute(TransactionCallback<T> action) throws TransactionException
}

@Override
public void execute(Runnable action) throws TransactionException {
public <T> T execute(Supplier<T> action) throws TransactionException {
return action.get();
}

@Override
public void executeWithoutResult(Runnable action) throws TransactionException {
action.run();
}

Expand Down

0 comments on commit b872fc5

Please sign in to comment.