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
We now correctly consider the transactional state along with the requested bindings scope when binding RedisConnection for reuse.
Previously, enabling transaction support on RedisTemplate registered the connection unconditionally with TransactionSynchronizationManager expecting a TransactionManager to close/unbind the connection upon transaction cleanup. This behavior could easily lead to lingering connections when a transactional RedisTemplate was used outside of a managed transaction.
We now distinguish between closure-scope binding and transaction binding.
Closure-scoped bindings (RedisTemplate.execute(SessionCallback)) do not interfere with transactional resources if there's no transactionally bound RedisConnection.
Closure-scope however reuses a transactionally bound connection.
Transaction-scoped binding binds only a connection to the transaction if there's an active transaction. Without an ongoing transaction, RedisTemplate does no longer bind connections to TransactionSynchronizationManager.
Original Pull Request: #573
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/redis-transactions.adoc
+14-5Lines changed: 14 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
[[tx]]
2
2
= Redis Transactions
3
3
4
-
Redis provides support for https://redis.io/topics/transactions[transactions] through the `multi`, `exec`, and `discard` commands.These operations are available on `RedisTemplate`.However, `RedisTemplate` is not guaranteed to run all the operations in the transaction with the same connection.
4
+
Redis provides support for https://redis.io/topics/transactions[transactions] through the `multi`, `exec`, and `discard` commands.These operations are available on `RedisTemplate`.However, `RedisTemplate` is not guaranteed to run all the operations in the transaction with the same connection.
5
5
6
-
Spring Data Redis provides the `SessionCallback` interface for use when multiple operations need to be performed with the same `connection`, such as when using Redis transactions.The following example uses the `multi` method:
6
+
Spring Data Redis provides the `SessionCallback` interface for use when multiple operations need to be performed with the same `connection`, such as when using Redis transactions.The following example uses the `multi` method:
System.out.println("Number of items added to set: " + txResults.get(0));
21
21
----
22
22
23
-
`RedisTemplate` uses its value, hash key, and hash value serializers to deserialize all results of `exec` before returning.There is an additional `exec` method that lets you pass a custom serializer for transaction results.
23
+
`RedisTemplate` uses its value, hash key, and hash value serializers to deserialize all results of `exec` before returning.There is an additional `exec` method that lets you pass a custom serializer for transaction results.
24
24
25
25
include::version-note.adoc[]
26
26
27
27
[[tx.spring]]
28
28
== @Transactional Support
29
29
30
-
By default, transaction Support is disabled and has to be explicitly enabled for each `RedisTemplate` in use by setting `setEnableTransactionSupport(true)`. Doing so forces binding the current `RedisConnection` to the current `Thread` that is triggering `MULTI`. If the transaction finishes without errors, `EXEC` is called. Otherwise `DISCARD` is called. Once in `MULTI`, `RedisConnection` queues write operations. All `readonly` operations, such as `KEYS`, are piped to a fresh (non-thread-bound) `RedisConnection`.
30
+
By default, `RedisTemplate` does not participate in managed Spring transactions.
31
+
If you want `RedisTemplate` to make use of Redis transaction when using `@Transactional` or `TransactionTemplate`, you need to be explicitly enable transaction support for each `RedisTemplate` by setting `setEnableTransactionSupport(true)`.
32
+
Enabling transaction support binds `RedisConnection` to the current transaction backed by a `ThreadLocal`.
33
+
If the transaction finishes without errors, the Redis transaction gets commited with `EXEC`, otherwise rolled back with `DISCARD`.
34
+
Redis transactions are batch-oriented.
35
+
Commands issued during an ongoing transaction are queued and only applied when committing the transaction.
36
+
37
+
Spring Data Redis distinguishes between read-only and write commands in an ongoing transaction.
38
+
Read-only commands, such as `KEYS`, are piped to a fresh (non-thread-bound) `RedisConnection` to allow reads.
39
+
Write commands are queued by `RedisTemplate` and applied upon commit.
31
40
32
41
The following example shows how to configure transaction management:
33
42
@@ -65,7 +74,7 @@ public class RedisTxContextConfiguration {
65
74
----
66
75
<1> Configures a Spring Context to enable https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/data-access.html#transaction-declarative[declarative transaction management].
67
76
<2> Configures `RedisTemplate` to participate in transactions by binding connections to the current thread.
68
-
<3> Transaction management requires a `PlatformTransactionManager`.Spring Data Redis does not ship with a `PlatformTransactionManager` implementation.Assuming your application uses JDBC, Spring Data Redis can participate in transactions by using existing transaction managers.
77
+
<3> Transaction management requires a `PlatformTransactionManager`.Spring Data Redis does not ship with a `PlatformTransactionManager` implementation.Assuming your application uses JDBC, Spring Data Redis can participate in transactions by using existing transaction managers.
69
78
====
70
79
71
80
The following examples each demonstrate a usage constraint:
0 commit comments