Skip to content

Commit 0be610b

Browse files
committed
Support for reactive transactions in TransactionInterceptor
Introduces TransactionManager marker interface for PlatformTransactionManager as well as ReactiveTransactionManager, allowing for a common configuration type in TransactionAspectSupport and TransactionManagementConfigurer. Closes gh-22590
1 parent 8dabb3e commit 0be610b

File tree

9 files changed

+919
-26
lines changed

9 files changed

+919
-26
lines changed

spring-test/src/main/java/org/springframework/test/context/transaction/TestContextTransactionUtils.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import org.springframework.lang.Nullable;
3030
import org.springframework.test.context.TestContext;
3131
import org.springframework.transaction.PlatformTransactionManager;
32+
import org.springframework.transaction.TransactionManager;
3233
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
3334
import org.springframework.transaction.interceptor.DelegatingTransactionAttribute;
3435
import org.springframework.transaction.interceptor.TransactionAttribute;
@@ -202,7 +203,14 @@ public static PlatformTransactionManager retrieveTransactionManager(TestContext
202203
Assert.state(configurers.size() <= 1,
203204
"Only one TransactionManagementConfigurer may exist in the ApplicationContext");
204205
if (configurers.size() == 1) {
205-
return configurers.values().iterator().next().annotationDrivenTransactionManager();
206+
TransactionManager tm = configurers.values().iterator().next().annotationDrivenTransactionManager();
207+
if (tm instanceof PlatformTransactionManager) {
208+
return (PlatformTransactionManager) tm;
209+
}
210+
else {
211+
throw new IllegalStateException(
212+
"Specified transaction manager is not a PlatformTransactionManager: " + tm);
213+
}
206214
}
207215
}
208216

spring-tx/src/main/java/org/springframework/transaction/PlatformTransactionManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@
4343
* @see org.springframework.transaction.interceptor.TransactionInterceptor
4444
* @see org.springframework.transaction.interceptor.TransactionProxyFactoryBean
4545
*/
46-
public interface PlatformTransactionManager {
46+
public interface PlatformTransactionManager extends TransactionManager {
4747

4848
/**
4949
* Return a currently active transaction or create a new one, according to

spring-tx/src/main/java/org/springframework/transaction/ReactiveTransactionManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
* @author Mark Paluch
2828
* @author Juergen Hoeller
2929
* @since 5.2
30-
* @see org.springframework.transaction.interceptor.TransactionProxyFactoryBean
3130
*/
32-
public interface ReactiveTransactionManager {
31+
public interface ReactiveTransactionManager extends TransactionManager {
3332

3433
/**
3534
* Emit a currently active reactive transaction or create a new one, according to
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.transaction;
18+
19+
/**
20+
* Marker interface for Spring transaction manager implementations,
21+
* either traditional or reactive.
22+
*
23+
* @author Juergen Hoeller
24+
* @since 5.2
25+
* @see PlatformTransactionManager
26+
* @see ReactiveTransactionManager
27+
*/
28+
public interface TransactionManager {
29+
30+
}

spring-tx/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@
2727
import org.springframework.core.annotation.AnnotationAttributes;
2828
import org.springframework.core.type.AnnotationMetadata;
2929
import org.springframework.lang.Nullable;
30-
import org.springframework.transaction.PlatformTransactionManager;
30+
import org.springframework.transaction.TransactionManager;
3131
import org.springframework.transaction.config.TransactionManagementConfigUtils;
3232
import org.springframework.transaction.event.TransactionalEventListenerFactory;
3333
import org.springframework.util.CollectionUtils;
@@ -51,7 +51,7 @@ public abstract class AbstractTransactionManagementConfiguration implements Impo
5151
* Default transaction manager, as configured through a {@link TransactionManagementConfigurer}.
5252
*/
5353
@Nullable
54-
protected PlatformTransactionManager txManager;
54+
protected TransactionManager txManager;
5555

5656

5757
@Override

spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurer.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,16 @@
1616

1717
package org.springframework.transaction.annotation;
1818

19-
import org.springframework.transaction.PlatformTransactionManager;
19+
import org.springframework.transaction.TransactionManager;
2020

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

@@ -76,7 +79,9 @@ public interface TransactionManagementConfigurer {
7679
* container as all {@code PlatformTransactionManager} implementations take advantage
7780
* of Spring lifecycle callbacks such as {@code InitializingBean} and
7881
* {@code BeanFactoryAware}.
82+
* @return a {@link org.springframework.transaction.PlatformTransactionManager} or
83+
* {@link org.springframework.transaction.ReactiveTransactionManager} implementation
7984
*/
80-
PlatformTransactionManager annotationDrivenTransactionManager();
85+
TransactionManager annotationDrivenTransactionManager();
8186

8287
}

0 commit comments

Comments
 (0)