|
72 | 72 | import org.springframework.kafka.test.context.EmbeddedKafka;
|
73 | 73 | import org.springframework.kafka.test.utils.KafkaTestUtils;
|
74 | 74 | import org.springframework.kafka.transaction.KafkaTransactionManager;
|
| 75 | +import org.springframework.transaction.TransactionDefinition; |
| 76 | +import org.springframework.transaction.TransactionException; |
75 | 77 | import org.springframework.transaction.annotation.EnableTransactionManagement;
|
76 | 78 | import org.springframework.transaction.annotation.Propagation;
|
77 | 79 | import org.springframework.transaction.annotation.Transactional;
|
78 | 80 | import org.springframework.transaction.support.AbstractPlatformTransactionManager;
|
| 81 | +import org.springframework.transaction.support.DefaultTransactionStatus; |
79 | 82 | import org.springframework.transaction.support.TransactionTemplate;
|
80 | 83 | import org.springframework.util.concurrent.SettableListenableFuture;
|
81 | 84 |
|
@@ -296,14 +299,15 @@ public void testTransactionSynchronizationExceptionOnCommit() {
|
296 | 299 |
|
297 | 300 | ResourcelessTransactionManager tm = new ResourcelessTransactionManager();
|
298 | 301 |
|
299 |
| - new TransactionTemplate(tm) |
300 |
| - .execute(s -> { |
301 |
| - template.sendDefault("foo", "bar"); |
| 302 | + assertThatExceptionOfType(ProducerFencedException.class).isThrownBy(() -> |
| 303 | + new TransactionTemplate(tm) |
| 304 | + .execute(s -> { |
| 305 | + template.sendDefault("foo", "bar"); |
302 | 306 |
|
303 |
| - // Mark the mock producer as fenced so it throws when committing the transaction |
304 |
| - producer.fenceProducer(); |
305 |
| - return null; |
306 |
| - }); |
| 307 | + // Mark the mock producer as fenced so it throws when committing the transaction |
| 308 | + producer.fenceProducer(); |
| 309 | + return null; |
| 310 | + })); |
307 | 311 |
|
308 | 312 | assertThat(producer.transactionCommitted()).isFalse();
|
309 | 313 | assertThat(producer.closed()).isTrue();
|
@@ -574,6 +578,28 @@ void testNonTxWithTx() {
|
574 | 578 | pf.destroy();
|
575 | 579 | }
|
576 | 580 |
|
| 581 | + @Test |
| 582 | + void syncCommitFails() { |
| 583 | + DummyTM tm = new DummyTM(); |
| 584 | + MockProducer<String, String> producer = |
| 585 | + new MockProducer<>(true, new StringSerializer(), new StringSerializer()); |
| 586 | + producer.initTransactions(); |
| 587 | + producer.commitTransactionException = new IllegalStateException(); |
| 588 | + |
| 589 | + @SuppressWarnings("unchecked") |
| 590 | + ProducerFactory<String, String> pf = mock(ProducerFactory.class); |
| 591 | + given(pf.transactionCapable()).willReturn(true); |
| 592 | + given(pf.createProducer(isNull())).willReturn(producer); |
| 593 | + |
| 594 | + KafkaTemplate<String, String> template = new KafkaTemplate<>(pf); |
| 595 | + template.setDefaultTopic(STRING_KEY_TOPIC); |
| 596 | + |
| 597 | + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> |
| 598 | + new TransactionTemplate(tm).execute(status -> template.sendDefault("foo"))); |
| 599 | + |
| 600 | + assertThat(tm.committed).isTrue(); |
| 601 | + } |
| 602 | + |
577 | 603 | @Configuration
|
578 | 604 | @EnableTransactionManagement
|
579 | 605 | public static class DeclarativeConfig {
|
@@ -681,4 +707,29 @@ public void anotherTxMethod() {
|
681 | 707 |
|
682 | 708 | }
|
683 | 709 |
|
| 710 | + @SuppressWarnings("serial") |
| 711 | + private static final class DummyTM extends AbstractPlatformTransactionManager { |
| 712 | + |
| 713 | + boolean committed; |
| 714 | + |
| 715 | + @Override |
| 716 | + protected Object doGetTransaction() throws TransactionException { |
| 717 | + return new Object(); |
| 718 | + } |
| 719 | + |
| 720 | + @Override |
| 721 | + protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException { |
| 722 | + } |
| 723 | + |
| 724 | + @Override |
| 725 | + protected void doCommit(DefaultTransactionStatus status) throws TransactionException { |
| 726 | + this.committed = true; |
| 727 | + } |
| 728 | + |
| 729 | + @Override |
| 730 | + protected void doRollback(DefaultTransactionStatus status) throws TransactionException { |
| 731 | + } |
| 732 | + |
| 733 | + } |
| 734 | + |
684 | 735 | }
|
0 commit comments