You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/reference/asciidoc/kafka.adoc
+17-38Lines changed: 17 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3050,6 +3050,8 @@ Spring for Apache Kafka adds support in the following ways:
3050
3050
* `KafkaTransactionManager`: Used with normal Spring transaction support (`@Transactional`, `TransactionTemplate` etc).
3051
3051
* Transactional `KafkaMessageListenerContainer`
3052
3052
* Local transactions with `KafkaTemplate`
3053
+
* Transaction synchronization with other transaction managers
3054
+
* `ChainedKafkaTransactionManager`
3053
3055
3054
3056
Transactions are enabled by providing the `DefaultKafkaProducerFactory` with a `transactionIdPrefix`.
3055
3057
In that case, instead of managing a single shared `Producer`, the factory maintains a cache of transactional producers.
@@ -3064,9 +3066,12 @@ NOTE: While transactions are supported with batch listeners, by default, zombie
3064
3066
However, starting with version 2.3.2, zombie fencing is supported if you set the container property `subBatchPerPartition` to true.
3065
3067
In that case, the batch listener is invoked once per partition received from the last poll, as if each poll only returned records for a single partition.
3066
3068
This is `true` by default since version 2.5 when transactions are enabled with `EOSMode.ALPHA`; set it to `false` if you are using transactions but are not concerned about zombie fencing.
3069
+
Also see <<exactly-once>>.
3067
3070
3068
3071
Also see <<transaction-id-prefix>>.
3069
3072
3073
+
With Spring Boot, it is only necessary to set the `spring.kafka.producer.transaction-id-prefix` property - Boot will automatically configure a `KafkaTransactionManager` bean and wire it into the listener container.
3074
+
3070
3075
===== Using `KafkaTransactionManager`
3071
3076
3072
3077
The `KafkaTransactionManager` is an implementation of Spring Framework's `PlatformTransactionManager`.
@@ -3081,52 +3086,24 @@ You must configure the `KafkaTemplate` to use the same `ProducerFactory` as the
3081
3086
3082
3087
===== Transaction Synchronization
3083
3088
3084
-
If you need to synchronize a Kafka transaction with some other transaction, configure the listener container with the appropriate transaction manager (one that supports synchronization, such as the `DataSourceTransactionManager`).
3085
-
Any operations performed on a transactional `KafkaTemplate` from the listener participate in a single transaction.
3086
-
The Kafka transaction is committed (or rolled back) immediately after the controlling transaction.
3087
-
Before exiting the listener, you should invoke one of the template's `sendOffsetsToTransaction` methods (unless you use a <<chained-transaction-manager,`ChainedKafkaTransactionManager`>>).
3088
-
For convenience, the listener container binds its consumer group ID to the thread, so, generally, you can use the first method.
3089
-
The following listing shows the two method signatures:
This section refers to producer-only transactions (transactions not started by a listener container); see <<chained-transaction-manager>> for information about synchronizing transactions when the container starts the transaction.
3099
3090
3100
-
The following example shows how to use the first signature of the `sendOffsetsToTransaction` method:
3091
+
If you want to send records to kafka and perform some database updates, you can use normal Spring transaction management with, say, a `DataSourceTransactionManager`.
NOTE: The offset to be committed is one greater than the offset of the records processed by the listener.
3125
-
3126
-
IMPORTANT: You should call this only when you use transaction synchronization.
3127
-
When a listener container is configured to use a `KafkaTransactionManager` or `ChainedKafkaTransactionManager`, it takes care of sending the offsets to the transaction.
3128
-
3129
-
See <<ex-jdbc-sync>> for an example application that synchronizes JDBC and Kafka transactions.
3104
+
The interceptor for the `@Transactional` annotation starts the transaction and the `KafkaTemplate` will synchronize a transaction with that transaction manager; each send will participate in that transaction.
3105
+
When the method exits, the database transaction will commit followed by the Kafka transaction.
3106
+
If you wish the commits to be performed in the reverse order (Kafka first), use a <<chained-transaction-manager,`ChainedTransactionManager`>> configured with the `DataSourceTransactionManager` as the first TM and then the `KafkaTransactionManager`, in that order.
3130
3107
3131
3108
[[chained-transaction-manager]]
3132
3109
===== Using `ChainedKafkaTransactionManager`
@@ -3137,7 +3114,7 @@ Since it is a `KafkaAwareTransactionManager`, the container can send the offsets
3137
3114
This provides another mechanism for synchronizing transactions without having to send the offsets to the transaction in the listener code.
3138
3115
You should chain your transaction managers in the desired order and provide the `ChainedTransactionManager` in the `ContainerProperties`.
3139
3116
3140
-
See <<ex-jdbc-sync>> for an example application that synchronizes JDBC and Kafka transactions.
3117
+
See <<ex-jdbc-sync>> for an example application that chains JDBC and Kafka transactions.
3141
3118
3142
3119
Starting with version 2.5.4, you can configure a `TransactionDefinition` in the `ContainerProperties`; its properties will be copied to the container's `TransactionTemplate` used to start transactions.
3143
3120
This allows, for example, setting a transaction timeout for other transaction managers within the `ChainedKafkaTransactionManager`.
@@ -3182,6 +3159,8 @@ This value should be the same for all application instances.
3182
3159
For transactions started by the template (or the transaction manager for `@Transaction`) you should set the property on the template and transaction manager respectively.
3183
3160
This property must have a different value on each application instance.
3184
3161
3162
+
This problem has been eliminated when `EOSMode.BETA` is being used (with broker versions >= 2.5); see <<exactly-once>>.
3163
+
3185
3164
[[tx-template-mixed]]
3186
3165
===== `KafkaTemplate` Transactional and non-Transactional Publishing
Copy file name to clipboardExpand all lines: src/reference/asciidoc/tips.adoc
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,7 @@ You should also set the container's `AckMode` to `MANUAL` to prevent the contain
47
47
Howewever, starting with version 2.5.5, as shown above, you can apply an initial offset to all partitions; see <<manual-assignment>> for more information.
48
48
49
49
[[ex-jdbc-sync]]
50
-
=== Example of Transaction Synchronization
50
+
=== Example of Transactions with ChainedKafkaTransactionManager
51
51
52
52
The following Spring Boot application is an example of synchronizing database and Kafka transactions.
0 commit comments