Class KafkaTransactionManager<K,V>
- Type Parameters:
K
- the key type.V
- the value type.
- All Implemented Interfaces:
Serializable
,KafkaAwareTransactionManager<K,
,V> ConfigurableTransactionManager
,PlatformTransactionManager
,TransactionManager
PlatformTransactionManager
implementation for a
single Kafka ProducerFactory
. Binds a Kafka producer from the specified
ProducerFactory to the thread, potentially allowing for one thread-bound producer per
ProducerFactory.
This local strategy is an alternative to executing Kafka operations within, and synchronized with, external transactions. This strategy is not able to provide XA transactions, for example in order to share transactions between messaging and database access.
Application code is required to retrieve the transactional Kafka resources via
ProducerFactoryUtils.getTransactionalResourceHolder(ProducerFactory, String, java.time.Duration)
.
Spring's KafkaTemplate
will auto
detect a thread-bound Producer and automatically participate in it.
The use of DefaultKafkaProducerFactory
as a target for this transaction manager is strongly
recommended. Because it caches producers for reuse.
Transaction synchronization is turned off by default, as this manager might be used alongside a datastore-based Spring transaction manager such as the JDBC org.springframework.jdbc.datasource.DataSourceTransactionManager, which has stronger needs for synchronization.
- Author:
- Gary Russell
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager
AbstractPlatformTransactionManager.SuspendedResourcesHolder
-
Field Summary
Fields inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager
logger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
-
Constructor Summary
ConstructorDescriptionKafkaTransactionManager
(ProducerFactory<K, V> producerFactory) Create a new KafkaTransactionManager, given a ProducerFactory. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
doBegin
(Object transaction, TransactionDefinition definition) protected void
doCleanupAfterCompletion
(Object transaction) protected void
doCommit
(DefaultTransactionStatus status) protected Object
protected void
protected void
doRollback
(DefaultTransactionStatus status) protected void
protected Object
Get the producer factory.protected boolean
isExistingTransaction
(Object transaction) void
setCloseTimeout
(Duration closeTimeout) Set the maximum time to wait when closing a producer; default 5 seconds.void
setTransactionIdPrefix
(String transactionIdPrefix) Set a transaction id prefix to override the prefix in the producer factory.Methods inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager
commit, determineTimeout, getDefaultTimeout, getTransaction, getTransactionExecutionListeners, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, prepareForCommit, prepareSynchronization, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionExecutionListeners, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransaction
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.transaction.ConfigurableTransactionManager
addListener
Methods inherited from interface org.springframework.transaction.PlatformTransactionManager
commit, getTransaction, rollback
-
Constructor Details
-
KafkaTransactionManager
Create a new KafkaTransactionManager, given a ProducerFactory. Transaction synchronization is turned off by default, as this manager might be used alongside a datastore-based Spring transaction manager like DataSourceTransactionManager, which has stronger needs for synchronization. Only one manager is allowed to drive synchronization at any point of time.- Parameters:
producerFactory
- the ProducerFactory to use
-
-
Method Details
-
setTransactionIdPrefix
Set a transaction id prefix to override the prefix in the producer factory.- Parameters:
transactionIdPrefix
- the prefix.- Since:
- 2.3
-
getProducerFactory
Get the producer factory.- Specified by:
getProducerFactory
in interfaceKafkaAwareTransactionManager<K,
V> - Returns:
- the producerFactory
-
setCloseTimeout
Set the maximum time to wait when closing a producer; default 5 seconds.- Parameters:
closeTimeout
- the close timeout.- Since:
- 2.1.14
-
doGetTransaction
- Specified by:
doGetTransaction
in classAbstractPlatformTransactionManager
-
isExistingTransaction
- Overrides:
isExistingTransaction
in classAbstractPlatformTransactionManager
-
doBegin
- Specified by:
doBegin
in classAbstractPlatformTransactionManager
-
doSuspend
- Overrides:
doSuspend
in classAbstractPlatformTransactionManager
-
doResume
- Overrides:
doResume
in classAbstractPlatformTransactionManager
-
doCommit
- Specified by:
doCommit
in classAbstractPlatformTransactionManager
-
doRollback
- Specified by:
doRollback
in classAbstractPlatformTransactionManager
-
doSetRollbackOnly
- Overrides:
doSetRollbackOnly
in classAbstractPlatformTransactionManager
-
doCleanupAfterCompletion
- Overrides:
doCleanupAfterCompletion
in classAbstractPlatformTransactionManager
-