From 2abff1a5b0f98e7c081197f5e5d121814ac9a173 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 5 Jul 2019 16:24:37 +0200 Subject: [PATCH] Introduce withoutTransaction factory method on TransactionOperations Closes gh-23198 --- .../support/TransactionOperations.java | 21 ++++++++- .../support/WithoutTransactionOperations.java | 43 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 spring-tx/src/main/java/org/springframework/transaction/support/WithoutTransactionOperations.java diff --git a/spring-tx/src/main/java/org/springframework/transaction/support/TransactionOperations.java b/spring-tx/src/main/java/org/springframework/transaction/support/TransactionOperations.java index bf70e1b6a09..af0d2050d22 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/support/TransactionOperations.java +++ b/spring-tx/src/main/java/org/springframework/transaction/support/TransactionOperations.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,4 +44,23 @@ public interface TransactionOperations { @Nullable T execute(TransactionCallback action) throws TransactionException; + + /** + * Return an implementation of the {@code TransactionOperations} interface which + * executes a given {@link TransactionCallback} without an actual transaction. + *

Useful for testing: The behavior is equivalent to running with a + * transaction manager with no actual transaction (PROPAGATION_SUPPORTS) + * and no synchronization (SYNCHRONIZATION_NEVER). + *

For a {@link TransactionOperations} implementation with actual + * transaction processing, use {@link TransactionTemplate} with an appropriate + * {@link org.springframework.transaction.PlatformTransactionManager}. + * @since 5.2 + * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_SUPPORTS + * @see AbstractPlatformTransactionManager#SYNCHRONIZATION_NEVER + * @see TransactionTemplate + */ + static TransactionOperations withoutTransaction() { + return WithoutTransactionOperations.INSTANCE; + }; + } diff --git a/spring-tx/src/main/java/org/springframework/transaction/support/WithoutTransactionOperations.java b/spring-tx/src/main/java/org/springframework/transaction/support/WithoutTransactionOperations.java new file mode 100644 index 00000000000..52a289ed5f1 --- /dev/null +++ b/spring-tx/src/main/java/org/springframework/transaction/support/WithoutTransactionOperations.java @@ -0,0 +1,43 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.transaction.support; + +import org.springframework.lang.Nullable; +import org.springframework.transaction.TransactionException; + +/** + * A {@link TransactionOperations} implementation which executes a given + * {@link TransactionCallback} without an actual transaction. + * + * @author Juergen Hoeller + * @since 5.2 + * @see TransactionOperations#withoutTransaction() + */ +final class WithoutTransactionOperations implements TransactionOperations { + + static final WithoutTransactionOperations INSTANCE = new WithoutTransactionOperations(); + + private WithoutTransactionOperations() { + } + + @Override + @Nullable + public T execute(TransactionCallback action) throws TransactionException { + return action.doInTransaction(new SimpleTransactionStatus(false)); + } + +}