|
1 | 1 | /* |
2 | | - * Copyright 2016-2020 the original author or authors. |
| 2 | + * Copyright 2016-2021 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
58 | 58 | import org.springframework.context.ApplicationListener; |
59 | 59 | import org.springframework.context.event.ContextStoppedEvent; |
60 | 60 | import org.springframework.core.log.LogAccessor; |
| 61 | +import org.springframework.kafka.KafkaException; |
61 | 62 | import org.springframework.kafka.support.TransactionSupport; |
62 | 63 | import org.springframework.lang.Nullable; |
63 | 64 | import org.springframework.util.Assert; |
@@ -715,7 +716,20 @@ private CloseSafeProducer<K, V> doCreateTxProducer(String prefix, String suffix, |
715 | 716 | } |
716 | 717 | checkBootstrap(newProducerConfigs); |
717 | 718 | newProducer = createRawProducer(newProducerConfigs); |
718 | | - newProducer.initTransactions(); |
| 719 | + try { |
| 720 | + newProducer.initTransactions(); |
| 721 | + } |
| 722 | + catch (RuntimeException ex) { |
| 723 | + try { |
| 724 | + newProducer.close(this.physicalCloseTimeout); |
| 725 | + } |
| 726 | + catch (RuntimeException ex2) { |
| 727 | + KafkaException newEx = new KafkaException("initTransactions() failed and then close() failed", ex); |
| 728 | + newEx.addSuppressed(ex2); |
| 729 | + throw newEx; // NOSONAR - lost stack trace |
| 730 | + } |
| 731 | + throw new KafkaException("initTransactions() failed", ex); |
| 732 | + } |
719 | 733 | CloseSafeProducer<K, V> closeSafeProducer = |
720 | 734 | new CloseSafeProducer<>(newProducer, remover, prefix, this.physicalCloseTimeout, this.beanName, |
721 | 735 | this.epoch.get()); |
|
0 commit comments