Skip to content

Commit

Permalink
Support for reactive transactions in TransactionInterceptor
Browse files Browse the repository at this point in the history
Introduces TransactionManager marker interface for PlatformTransactionManager as well as ReactiveTransactionManager, allowing for a common configuration type in TransactionAspectSupport and TransactionManagementConfigurer.

Closes gh-22590
  • Loading branch information
jhoeller committed May 2, 2019
1 parent 8dabb3e commit 0be610b
Show file tree
Hide file tree
Showing 9 changed files with 919 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -29,6 +29,7 @@
import org.springframework.lang.Nullable;
import org.springframework.test.context.TestContext;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import org.springframework.transaction.interceptor.DelegatingTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
Expand Down Expand Up @@ -202,7 +203,14 @@ public static PlatformTransactionManager retrieveTransactionManager(TestContext
Assert.state(configurers.size() <= 1,
"Only one TransactionManagementConfigurer may exist in the ApplicationContext");
if (configurers.size() == 1) {
return configurers.values().iterator().next().annotationDrivenTransactionManager();
TransactionManager tm = configurers.values().iterator().next().annotationDrivenTransactionManager();
if (tm instanceof PlatformTransactionManager) {
return (PlatformTransactionManager) tm;
}
else {
throw new IllegalStateException(
"Specified transaction manager is not a PlatformTransactionManager: " + tm);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -43,7 +43,7 @@
* @see org.springframework.transaction.interceptor.TransactionInterceptor
* @see org.springframework.transaction.interceptor.TransactionProxyFactoryBean
*/
public interface PlatformTransactionManager {
public interface PlatformTransactionManager extends TransactionManager {

/**
* Return a currently active transaction or create a new one, according to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
* @author Mark Paluch
* @author Juergen Hoeller
* @since 5.2
* @see org.springframework.transaction.interceptor.TransactionProxyFactoryBean
*/
public interface ReactiveTransactionManager {
public interface ReactiveTransactionManager extends TransactionManager {

/**
* Emit a currently active reactive transaction or create a new one, according to
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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;

/**
* Marker interface for Spring transaction manager implementations,
* either traditional or reactive.
*
* @author Juergen Hoeller
* @since 5.2
* @see PlatformTransactionManager
* @see ReactiveTransactionManager
*/
public interface TransactionManager {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -27,7 +27,7 @@
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.lang.Nullable;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.config.TransactionManagementConfigUtils;
import org.springframework.transaction.event.TransactionalEventListenerFactory;
import org.springframework.util.CollectionUtils;
Expand All @@ -51,7 +51,7 @@ public abstract class AbstractTransactionManagementConfiguration implements Impo
* Default transaction manager, as configured through a {@link TransactionManagementConfigurer}.
*/
@Nullable
protected PlatformTransactionManager txManager;
protected TransactionManager txManager;


@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 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.
Expand All @@ -16,15 +16,16 @@

package org.springframework.transaction.annotation;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;

/**
* Interface to be implemented by @{@link org.springframework.context.annotation.Configuration
* Configuration} classes annotated with @{@link EnableTransactionManagement} that wish to
* or need to explicitly specify the default {@link PlatformTransactionManager} bean to be
* used for annotation-driven transaction management, as opposed to the default approach
* of a by-type lookup. One reason this might be necessary is if there are two
* {@code PlatformTransactionManager} beans present in the container.
* (or need to) explicitly specify the default {@code PlatformTransactionManager} bean
* (or {@code ReactiveTransactionManager} bean) to be used for annotation-driven
* transaction management, as opposed to the default approach of a by-type lookup.
* One reason this might be necessary is if there are two {@code PlatformTransactionManager}
* beans present in the container.
*
* <p>See @{@link EnableTransactionManagement} for general examples and context;
* see {@link #annotationDrivenTransactionManager()} for detailed instructions.
Expand All @@ -40,6 +41,8 @@
* @since 3.1
* @see EnableTransactionManagement
* @see org.springframework.context.annotation.Primary
* @see org.springframework.transaction.PlatformTransactionManager
* @see org.springframework.transaction.ReactiveTransactionManager
*/
public interface TransactionManagementConfigurer {

Expand Down Expand Up @@ -76,7 +79,9 @@ public interface TransactionManagementConfigurer {
* container as all {@code PlatformTransactionManager} implementations take advantage
* of Spring lifecycle callbacks such as {@code InitializingBean} and
* {@code BeanFactoryAware}.
* @return a {@link org.springframework.transaction.PlatformTransactionManager} or
* {@link org.springframework.transaction.ReactiveTransactionManager} implementation
*/
PlatformTransactionManager annotationDrivenTransactionManager();
TransactionManager annotationDrivenTransactionManager();

}
Loading

0 comments on commit 0be610b

Please sign in to comment.