Class KafkaTransactionManager<K,​V>

  • Type Parameters:
    K - the key type.
    V - the value type.
    All Implemented Interfaces:, KafkaAwareTransactionManager<K,​V>, org.springframework.transaction.PlatformTransactionManager, org.springframework.transaction.TransactionManager

    public class KafkaTransactionManager<K,​V>
    implements KafkaAwareTransactionManager<K,​V>
    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.

    Gary Russell
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class
    • Field Summary

      • Fields inherited from class

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void doBegin​(java.lang.Object transaction, org.springframework.transaction.TransactionDefinition definition)  
      protected void doCleanupAfterCompletion​(java.lang.Object transaction)  
      protected void doCommit​( status)  
      protected java.lang.Object doGetTransaction()  
      protected void doResume​(java.lang.Object transaction, java.lang.Object suspendedResources)  
      protected void doRollback​( status)  
      protected void doSetRollbackOnly​( status)  
      protected java.lang.Object doSuspend​(java.lang.Object transaction)  
      ProducerFactory<K,​V> getProducerFactory()
      Get the producer factory.
      protected boolean isExistingTransaction​(java.lang.Object transaction)  
      void setCloseTimeout​(java.time.Duration closeTimeout)
      Set the maximum time to wait when closing a producer; default 5 seconds.
      void setTransactionIdPrefix​(java.lang.String transactionIdPrefix)
      Set a transaction id prefix to override the prefix in the producer factory.
      • Methods inherited from class

        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
      • 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.PlatformTransactionManager

        commit, getTransaction, rollback
    • 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.
        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.
        transactionIdPrefix - the prefix.
      • setCloseTimeout

        public void setCloseTimeout​(java.time.Duration closeTimeout)
        Set the maximum time to wait when closing a producer; default 5 seconds.
        closeTimeout - the close timeout.
      • doGetTransaction

        protected java.lang.Object doGetTransaction()
        Specified by:
        doGetTransaction in class
      • isExistingTransaction

        protected boolean isExistingTransaction​(java.lang.Object transaction)
        isExistingTransaction in class
      • doBegin

        protected void doBegin​(java.lang.Object transaction,
                               org.springframework.transaction.TransactionDefinition definition)
        Specified by:
        doBegin in class
      • doSuspend

        protected java.lang.Object doSuspend​(java.lang.Object transaction)
        doSuspend in class
      • doResume

        protected void doResume​(java.lang.Object transaction,
                                java.lang.Object suspendedResources)
        doResume in class
      • doCommit

        protected void doCommit​( status)
        Specified by:
        doCommit in class
      • doRollback

        protected void doRollback​( status)
        Specified by:
        doRollback in class
      • doSetRollbackOnly

        protected void doSetRollbackOnly​( status)
        doSetRollbackOnly in class
      • doCleanupAfterCompletion

        protected void doCleanupAfterCompletion​(java.lang.Object transaction)
        doCleanupAfterCompletion in class