-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1309: Don't Bind Resource When Sync Not Active
Resolves #1309 If the `RabbitTemplate` was called from a `@TransactionalEventListener`, with phase `AFTER_COMMIT`, a transaction is "active", but synchronizations are already cleared. It is too late to synchronize this transaction. We end up with an orphaned `ResourceHolder` with pending transaction commits. Don't bind the resource holder if synchronization is not active. However, the proper solution, if users want to synchronize the rabbit transaction with the global transaction, is to use the `BEFORE_COMMIT` phase. See the discussion on the Github issue for more information. **cherry-pick to 2.2.x** * Fix since version. * Reset physical close required flag left over from another test. * Capture test results for all modules. # Conflicts: # .github/workflows/pr-build-workflow.yml # spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/ClientRecoveryCompatibilityTests.java
- Loading branch information
1 parent
aa6ae4f
commit bd7656c
Showing
3 changed files
with
158 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...t/src/test/java/org/springframework/amqp/rabbit/core/TransactionalEventListenerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright 2021 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.amqp.rabbit.core; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.amqp.rabbit.connection.Connection; | ||
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.TransactionDefinition; | ||
import org.springframework.transaction.TransactionException; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.transaction.event.TransactionPhase; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
import org.springframework.transaction.support.AbstractPlatformTransactionManager; | ||
import org.springframework.transaction.support.DefaultTransactionStatus; | ||
|
||
import com.rabbitmq.client.Channel; | ||
|
||
/** | ||
* @author Gary Russell | ||
* @since 2.2.16 | ||
* | ||
*/ | ||
@SpringJUnitConfig | ||
@DirtiesContext | ||
public class TransactionalEventListenerTests { | ||
|
||
@Test | ||
void txCommits(@Autowired Config config, @Autowired AtomicBoolean committed, | ||
@Autowired Channel channel) throws IOException { | ||
|
||
config.publish(); | ||
assertThat(committed.get()).isTrue(); | ||
verify(channel).txCommit(); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@EnableTransactionManagement | ||
public static class Config { | ||
|
||
@Autowired | ||
ApplicationEventPublisher publisher; | ||
|
||
@Autowired | ||
RabbitTemplate template; | ||
|
||
@Bean | ||
AtomicBoolean committed() { | ||
return new AtomicBoolean(); | ||
} | ||
|
||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
public void el(String in) { | ||
this.template.convertAndSend("test"); | ||
} | ||
|
||
@Bean | ||
RabbitTemplate template(ConnectionFactory cf) { | ||
RabbitTemplate template = new RabbitTemplate(cf); | ||
template.setChannelTransacted(true); | ||
return template; | ||
} | ||
|
||
@Bean | ||
Channel channel() { | ||
return mock(Channel.class); | ||
} | ||
|
||
@Bean | ||
ConnectionFactory cf(Channel channel) { | ||
ConnectionFactory cf = mock(ConnectionFactory.class); | ||
Connection conn = mock(Connection.class); | ||
given(conn.isOpen()).willReturn(true); | ||
given(cf.createConnection()).willReturn(conn); | ||
given(conn.createChannel(true)).willReturn(channel); | ||
given(channel.isOpen()).willReturn(true); | ||
return cf; | ||
} | ||
|
||
@Transactional | ||
public void publish() { | ||
publisher.publishEvent("test"); | ||
} | ||
|
||
@Bean | ||
PlatformTransactionManager transactionManager(AtomicBoolean committed) { | ||
return new AbstractPlatformTransactionManager() { | ||
|
||
@Override | ||
protected void doRollback(DefaultTransactionStatus status) throws TransactionException { | ||
} | ||
|
||
@Override | ||
protected Object doGetTransaction() throws TransactionException { | ||
return new Object(); | ||
} | ||
|
||
@Override | ||
protected void doCommit(DefaultTransactionStatus status) throws TransactionException { | ||
committed.set(true); | ||
} | ||
|
||
@Override | ||
protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException { | ||
} | ||
}; | ||
} | ||
|
||
} | ||
|
||
} |