Skip to content

Commit

Permalink
GH-1727: Close Producer if initTransactions Fails
Browse files Browse the repository at this point in the history
Resolves #1727
  • Loading branch information
garyrussell committed Mar 3, 2021
1 parent e48b639 commit c33b0b3
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-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.
Expand Down Expand Up @@ -58,6 +58,7 @@
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.core.log.LogAccessor;
import org.springframework.kafka.KafkaException;
import org.springframework.kafka.support.TransactionSupport;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -716,7 +717,20 @@ private CloseSafeProducer<K, V> doCreateTxProducer(String prefix, String suffix,
}
checkBootstrap(newProducerConfigs);
newProducer = createRawProducer(newProducerConfigs);
newProducer.initTransactions();
try {
newProducer.initTransactions();
}
catch (RuntimeException ex) {
try {
newProducer.close(this.physicalCloseTimeout);
}
catch (RuntimeException ex2) {
KafkaException newEx = new KafkaException("initTransactions() failed and then close() failed", ex);
newEx.addSuppressed(ex2);
throw newEx; // NOSONAR - lost stack trace
}
throw new KafkaException("initTransactions() failed", ex);
}
CloseSafeProducer<K, V> closeSafeProducer =
new CloseSafeProducer<>(newProducer, remover, prefix, this.physicalCloseTimeout, this.beanName,
this.epoch.get());
Expand Down

0 comments on commit c33b0b3

Please sign in to comment.