Skip to content

Commit b24ac74

Browse files
committed
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
1 parent 1e0bdc0 commit b24ac74

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,22 @@ public void testCustomCacheManager() {
8383

8484
@Test
8585
public void testEvictWithTransaction() {
86-
txTemplate.execute(() -> testEvict(this.cs, false));
86+
txTemplate.executeWithoutResult(s -> testEvict(this.cs, false));
8787
}
8888

8989
@Test
9090
public void testEvictEarlyWithTransaction() {
91-
txTemplate.execute(() -> testEvictEarly(this.cs));
91+
txTemplate.executeWithoutResult(s -> testEvictEarly(this.cs));
9292
}
9393

9494
@Test
9595
public void testEvictAllWithTransaction() {
96-
txTemplate.execute(() -> testEvictAll(this.cs, false));
96+
txTemplate.executeWithoutResult(s -> testEvictAll(this.cs, false));
9797
}
9898

9999
@Test
100100
public void testEvictAllEarlyWithTransaction() {
101-
txTemplate.execute(() -> testEvictAllEarly(this.cs));
101+
txTemplate.executeWithoutResult(s -> testEvictAllEarly(this.cs));
102102
}
103103

104104

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void putTransactional() {
7979
Cache cache = new TransactionAwareCacheDecorator(target);
8080
Object key = new Object();
8181

82-
txTemplate.execute(() -> {
82+
txTemplate.executeWithoutResult(s -> {
8383
cache.put(key, "123");
8484
assertThat(target.get(key)).isNull();
8585
});
@@ -106,7 +106,7 @@ public void putIfAbsentTransactional() { // no transactional support for putIfA
106106
Cache cache = new TransactionAwareCacheDecorator(target);
107107
Object key = new Object();
108108

109-
txTemplate.execute(() -> {
109+
txTemplate.executeWithoutResult(s -> {
110110
assertThat(cache.putIfAbsent(key, "123")).isNull();
111111
assertThat(target.get(key, String.class)).isEqualTo("123");
112112
assertThat(cache.putIfAbsent(key, "456").get()).isEqualTo("123");
@@ -135,7 +135,7 @@ public void evictTransactional() {
135135
Object key = new Object();
136136
cache.put(key, "123");
137137

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

164-
txTemplate.execute(() -> {
164+
txTemplate.executeWithoutResult(s -> {
165165
cache.evictIfPresent(key);
166166
assertThat(target.get(key)).isNull();
167167
});
@@ -187,7 +187,7 @@ public void clearTransactional() {
187187
Object key = new Object();
188188
cache.put(key, "123");
189189

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

216-
txTemplate.execute(() -> {
216+
txTemplate.executeWithoutResult(s -> {
217217
cache.invalidate();
218218
assertThat(target.get(key)).isNull();
219219
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private void executeSqlScripts(
260260
TransactionDefinition.PROPAGATION_REQUIRED);
261261
TransactionAttribute txAttr = TestContextTransactionUtils.createDelegatingTransactionAttribute(
262262
testContext, new DefaultTransactionAttribute(propagation));
263-
new TransactionTemplate(txMgr, txAttr).execute(() -> populator.execute(finalDataSource));
263+
new TransactionTemplate(txMgr, txAttr).executeWithoutResult(s -> populator.execute(finalDataSource));
264264
}
265265
}
266266

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
package org.springframework.transaction.support;
1818

19+
import java.util.function.Consumer;
20+
1921
import org.springframework.lang.Nullable;
2022
import org.springframework.transaction.TransactionException;
23+
import org.springframework.transaction.TransactionStatus;
2124

2225
/**
2326
* Interface specifying basic transaction execution operations.
@@ -40,6 +43,7 @@ public interface TransactionOperations {
4043
* @return a result object returned by the callback, or {@code null} if none
4144
* @throws TransactionException in case of initialization, rollback, or system errors
4245
* @throws RuntimeException if thrown by the TransactionCallback
46+
* @see #executeWithoutResult(Consumer)
4347
*/
4448
@Nullable
4549
<T> T execute(TransactionCallback<T> action) throws TransactionException;
@@ -59,9 +63,9 @@ public interface TransactionOperations {
5963
* @see #execute(TransactionCallback)
6064
* @see TransactionCallbackWithoutResult
6165
*/
62-
default void execute(Runnable action) throws TransactionException {
66+
default void executeWithoutResult(Consumer<TransactionStatus> action) throws TransactionException {
6367
execute(status -> {
64-
action.run();
68+
action.accept(status);
6569
return null;
6670
});
6771
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
package org.springframework.transaction.support;
1818

19+
import java.util.function.Consumer;
20+
1921
import org.springframework.lang.Nullable;
2022
import org.springframework.transaction.TransactionException;
23+
import org.springframework.transaction.TransactionStatus;
2124

2225
/**
2326
* A {@link TransactionOperations} implementation which executes a given
@@ -43,8 +46,8 @@ public <T> T execute(TransactionCallback<T> action) throws TransactionException
4346
}
4447

4548
@Override
46-
public void execute(Runnable action) throws TransactionException {
47-
action.run();
49+
public void executeWithoutResult(Consumer<TransactionStatus> action) throws TransactionException {
50+
action.accept(new SimpleTransactionStatus(false));
4851
}
4952

5053
}

0 commit comments

Comments
 (0)