Introduce withoutTransaction factory method on TransactionOperations

Closes gh-23198
This commit is contained in:
Juergen Hoeller 2019-07-05 16:24:37 +02:00
parent 7bfe01a028
commit 2abff1a5b0
2 changed files with 63 additions and 1 deletions

View File

@ -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> T execute(TransactionCallback<T> action) throws TransactionException;
/**
* Return an implementation of the {@code TransactionOperations} interface which
* executes a given {@link TransactionCallback} without an actual transaction.
* <p>Useful for testing: The behavior is equivalent to running with a
* transaction manager with no actual transaction (PROPAGATION_SUPPORTS)
* and no synchronization (SYNCHRONIZATION_NEVER).
* <p>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;
};
}

View File

@ -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> T execute(TransactionCallback<T> action) throws TransactionException {
return action.doInTransaction(new SimpleTransactionStatus(false));
}
}