Skip to content

Commit dd16e01

Browse files
committed
Clarify ReactiveTransactionManager exception declarations
Avoid misleading "throws TransactionException" declarations but preserve javadoc "@throws" notes for specific exceptions (with reactive propagation semantics). Closes gh-30817
1 parent d4cd358 commit dd16e01

File tree

3 files changed

+41
-57
lines changed

3 files changed

+41
-57
lines changed

spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/R2dbcTransactionManager.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.springframework.lang.Nullable;
3030
import org.springframework.transaction.CannotCreateTransactionException;
3131
import org.springframework.transaction.TransactionDefinition;
32-
import org.springframework.transaction.TransactionException;
3332
import org.springframework.transaction.reactive.AbstractReactiveTransactionManager;
3433
import org.springframework.transaction.reactive.GenericReactiveTransaction;
3534
import org.springframework.transaction.reactive.TransactionSynchronizationManager;
@@ -162,7 +161,7 @@ public void afterPropertiesSet() {
162161
}
163162

164163
@Override
165-
protected Object doGetTransaction(TransactionSynchronizationManager synchronizationManager) throws TransactionException {
164+
protected Object doGetTransaction(TransactionSynchronizationManager synchronizationManager) {
166165
ConnectionFactoryTransactionObject txObject = new ConnectionFactoryTransactionObject();
167166
ConnectionHolder conHolder = (ConnectionHolder) synchronizationManager.getResource(obtainConnectionFactory());
168167
txObject.setConnectionHolder(conHolder, false);
@@ -178,7 +177,7 @@ protected boolean isExistingTransaction(Object transaction) {
178177
@SuppressWarnings("deprecation")
179178
@Override
180179
protected Mono<Void> doBegin(TransactionSynchronizationManager synchronizationManager, Object transaction,
181-
TransactionDefinition definition) throws TransactionException {
180+
TransactionDefinition definition) {
182181

183182
ConnectionFactoryTransactionObject txObject = (ConnectionFactoryTransactionObject) transaction;
184183

@@ -243,9 +242,7 @@ protected Duration determineTimeout(TransactionDefinition definition) {
243242
}
244243

245244
@Override
246-
protected Mono<Object> doSuspend(TransactionSynchronizationManager synchronizationManager, Object transaction)
247-
throws TransactionException {
248-
245+
protected Mono<Object> doSuspend(TransactionSynchronizationManager synchronizationManager, Object transaction) {
249246
return Mono.defer(() -> {
250247
ConnectionFactoryTransactionObject txObject = (ConnectionFactoryTransactionObject) transaction;
251248
txObject.setConnectionHolder(null);
@@ -255,7 +252,7 @@ protected Mono<Object> doSuspend(TransactionSynchronizationManager synchronizati
255252

256253
@Override
257254
protected Mono<Void> doResume(TransactionSynchronizationManager synchronizationManager,
258-
@Nullable Object transaction, Object suspendedResources) throws TransactionException {
255+
@Nullable Object transaction, Object suspendedResources) {
259256

260257
return Mono.defer(() -> {
261258
synchronizationManager.bindResource(obtainConnectionFactory(), suspendedResources);
@@ -265,7 +262,7 @@ protected Mono<Void> doResume(TransactionSynchronizationManager synchronizationM
265262

266263
@Override
267264
protected Mono<Void> doCommit(TransactionSynchronizationManager TransactionSynchronizationManager,
268-
GenericReactiveTransaction status) throws TransactionException {
265+
GenericReactiveTransaction status) {
269266

270267
ConnectionFactoryTransactionObject txObject = (ConnectionFactoryTransactionObject) status.getTransaction();
271268
Connection connection = txObject.getConnectionHolder().getConnection();
@@ -278,7 +275,7 @@ protected Mono<Void> doCommit(TransactionSynchronizationManager TransactionSynch
278275

279276
@Override
280277
protected Mono<Void> doRollback(TransactionSynchronizationManager TransactionSynchronizationManager,
281-
GenericReactiveTransaction status) throws TransactionException {
278+
GenericReactiveTransaction status) {
282279

283280
ConnectionFactoryTransactionObject txObject = (ConnectionFactoryTransactionObject) status.getTransaction();
284281
Connection connection = txObject.getConnectionHolder().getConnection();
@@ -291,7 +288,7 @@ protected Mono<Void> doRollback(TransactionSynchronizationManager TransactionSyn
291288

292289
@Override
293290
protected Mono<Void> doSetRollbackOnly(TransactionSynchronizationManager synchronizationManager,
294-
GenericReactiveTransaction status) throws TransactionException {
291+
GenericReactiveTransaction status) {
295292

296293
return Mono.fromRunnable(() -> {
297294
ConnectionFactoryTransactionObject txObject = (ConnectionFactoryTransactionObject) status.getTransaction();

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -46,6 +46,8 @@ public interface ReactiveTransactionManager extends TransactionManager {
4646
* <p>An exception to the above rule is the read-only flag, which should be
4747
* ignored if no explicit read-only mode is supported. Essentially, the
4848
* read-only flag is just a hint for potential optimization.
49+
* <p>Note: In contrast to {@link PlatformTransactionManager}, exceptions
50+
* are propagated through the reactive pipeline returned from this method.
4951
* @param definition the TransactionDefinition instance,
5052
* describing propagation behavior, isolation level, timeout etc.
5153
* @return transaction status object representing the new or current transaction
@@ -58,8 +60,7 @@ public interface ReactiveTransactionManager extends TransactionManager {
5860
* @see TransactionDefinition#getTimeout
5961
* @see TransactionDefinition#isReadOnly
6062
*/
61-
Mono<ReactiveTransaction> getReactiveTransaction(@Nullable TransactionDefinition definition)
62-
throws TransactionException;
63+
Mono<ReactiveTransaction> getReactiveTransaction(@Nullable TransactionDefinition definition);
6364

6465
/**
6566
* Commit the given transaction, with regard to its status. If the transaction
@@ -69,14 +70,12 @@ Mono<ReactiveTransaction> getReactiveTransaction(@Nullable TransactionDefinition
6970
* has been suspended to be able to create a new one, resume the previous
7071
* transaction after committing the new one.
7172
* <p>Note that when the commit call completes, no matter if normally or
72-
* throwing an exception, the transaction must be fully completed and
73+
* propagating an exception, the transaction must be fully completed and
7374
* cleaned up. No rollback call should be expected in such a case.
74-
* <p>If this method throws an exception other than a TransactionException,
75-
* then some before-commit error caused the commit attempt to fail. For
76-
* example, an O/R Mapping tool might have tried to flush changes to the
77-
* database right before commit, with the resulting DataAccessException
78-
* causing the transaction to fail. The original exception will be
79-
* propagated to the caller of this commit method in such a case.
75+
* <p>Note: In contrast to {@link PlatformTransactionManager}, exceptions
76+
* are propagated through the reactive pipeline returned from this method.
77+
* Also, depending on the transaction manager implementation, {@code commit}
78+
* may propagate {@link org.springframework.dao.DataAccessException} as well.
8079
* @param transaction object returned by the {@code getTransaction} method
8180
* @throws UnexpectedRollbackException in case of an unexpected rollback
8281
* that the transaction coordinator initiated
@@ -88,24 +87,28 @@ Mono<ReactiveTransaction> getReactiveTransaction(@Nullable TransactionDefinition
8887
* is already completed (that is, committed or rolled back)
8988
* @see ReactiveTransaction#setRollbackOnly
9089
*/
91-
Mono<Void> commit(ReactiveTransaction transaction) throws TransactionException;
90+
Mono<Void> commit(ReactiveTransaction transaction);
9291

9392
/**
9493
* Perform a rollback of the given transaction.
9594
* <p>If the transaction wasn't a new one, just set it rollback-only for proper
9695
* participation in the surrounding transaction. If a previous transaction
9796
* has been suspended to be able to create a new one, resume the previous
9897
* transaction after rolling back the new one.
99-
* <p><b>Do not call rollback on a transaction if commit threw an exception.</b>
98+
* <p><b>Do not call rollback on a transaction if commit failed.</b>
10099
* The transaction will already have been completed and cleaned up when commit
101100
* returns, even in case of a commit exception. Consequently, a rollback call
102101
* after commit failure will lead to an IllegalTransactionStateException.
102+
* <p>Note: In contrast to {@link PlatformTransactionManager}, exceptions
103+
* are propagated through the reactive pipeline returned from this method.
104+
* Also, depending on the transaction manager implementation, {@code rollback}
105+
* may propagate {@link org.springframework.dao.DataAccessException} as well.
103106
* @param transaction object returned by the {@code getTransaction} method
104107
* @throws TransactionSystemException in case of rollback or system errors
105108
* (typically caused by fundamental resource failures)
106109
* @throws IllegalTransactionStateException if the given transaction
107110
* is already completed (that is, committed or rolled back)
108111
*/
109-
Mono<Void> rollback(ReactiveTransaction transaction) throws TransactionException;
112+
Mono<Void> rollback(ReactiveTransaction transaction);
110113

111114
}

0 commit comments

Comments
 (0)