Class KafkaTransactionManager<K,V>
- java.lang.Object
-
- org.springframework.transaction.support.AbstractPlatformTransactionManager
-
- org.springframework.kafka.transaction.KafkaTransactionManager<K,V>
-
- Type Parameters:
K- the key type.V- the value type.
- All Implemented Interfaces:
java.io.Serializable,KafkaAwareTransactionManager<K,V>,org.springframework.transaction.PlatformTransactionManager,org.springframework.transaction.TransactionManager
public class KafkaTransactionManager<K,V> extends org.springframework.transaction.support.AbstractPlatformTransactionManager implements KafkaAwareTransactionManager<K,V>
PlatformTransactionManagerimplementation for a single KafkaProducerFactory. 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'sKafkaTemplatewill auto detect a thread-bound Producer and automatically participate in it.The use of
DefaultKafkaProducerFactoryas 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:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description KafkaTransactionManager(ProducerFactory<K,V> producerFactory)Create a new KafkaTransactionManager, given a ProducerFactory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voiddoBegin(java.lang.Object transaction, org.springframework.transaction.TransactionDefinition definition)protected voiddoCleanupAfterCompletion(java.lang.Object transaction)protected voiddoCommit(org.springframework.transaction.support.DefaultTransactionStatus status)protected java.lang.ObjectdoGetTransaction()protected voiddoResume(java.lang.Object transaction, java.lang.Object suspendedResources)protected voiddoRollback(org.springframework.transaction.support.DefaultTransactionStatus status)protected voiddoSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status)protected java.lang.ObjectdoSuspend(java.lang.Object transaction)ProducerFactory<K,V>getProducerFactory()Get the producer factory.protected booleanisExistingTransaction(java.lang.Object transaction)voidsetCloseTimeout(java.time.Duration closeTimeout)Set the maximum time to wait when closing a producer; default 5 seconds.voidsetTransactionIdPrefix(java.lang.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, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, newTransactionStatus, prepareForCommit, prepareSynchronization, prepareTransactionStatus, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransaction
-
-
-
-
Constructor Detail
-
KafkaTransactionManager
public KafkaTransactionManager(ProducerFactory<K,V> producerFactory)
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 Detail
-
setTransactionIdPrefix
public void setTransactionIdPrefix(java.lang.String transactionIdPrefix)
Set a transaction id prefix to override the prefix in the producer factory.- Parameters:
transactionIdPrefix- the prefix.- Since:
- 2.3
-
getProducerFactory
public ProducerFactory<K,V> getProducerFactory()
Get the producer factory.- Specified by:
getProducerFactoryin interfaceKafkaAwareTransactionManager<K,V>- Returns:
- the producerFactory
-
setCloseTimeout
public void setCloseTimeout(java.time.Duration closeTimeout)
Set the maximum time to wait when closing a producer; default 5 seconds.- Parameters:
closeTimeout- the close timeout.- Since:
- 2.1.14
-
doGetTransaction
protected java.lang.Object doGetTransaction()
- Specified by:
doGetTransactionin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
isExistingTransaction
protected boolean isExistingTransaction(java.lang.Object transaction)
- Overrides:
isExistingTransactionin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doBegin
protected void doBegin(java.lang.Object transaction, org.springframework.transaction.TransactionDefinition definition)- Specified by:
doBeginin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doSuspend
protected java.lang.Object doSuspend(java.lang.Object transaction)
- Overrides:
doSuspendin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doResume
protected void doResume(java.lang.Object transaction, java.lang.Object suspendedResources)- Overrides:
doResumein classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doCommit
protected void doCommit(org.springframework.transaction.support.DefaultTransactionStatus status)
- Specified by:
doCommitin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doRollback
protected void doRollback(org.springframework.transaction.support.DefaultTransactionStatus status)
- Specified by:
doRollbackin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doSetRollbackOnly
protected void doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status)
- Overrides:
doSetRollbackOnlyin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
doCleanupAfterCompletion
protected void doCleanupAfterCompletion(java.lang.Object transaction)
- Overrides:
doCleanupAfterCompletionin classorg.springframework.transaction.support.AbstractPlatformTransactionManager
-
-