From 61be399dcb56e85d6a96e56b6d823d203af3e49b Mon Sep 17 00:00:00 2001 From: Egor Ponomarev <19322181+egor-ponomarev@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:45:24 -0500 Subject: [PATCH 1/2] Update documentation of transactions page --- .../ROOT/pages/redis/transactions.adoc | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/antora/modules/ROOT/pages/redis/transactions.adoc b/src/main/antora/modules/ROOT/pages/redis/transactions.adoc index c8b8720888..f017592836 100644 --- a/src/main/antora/modules/ROOT/pages/redis/transactions.adoc +++ b/src/main/antora/modules/ROOT/pages/redis/transactions.adoc @@ -25,6 +25,31 @@ System.out.println("Number of items added to set: " + txResults.get(0)); `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. +It worth to mention that if between the commands multi() and exec() a query timeout exception happens (e.g. in case +Redis is not available to respond fast) then the connection may get stuck in a transactional state. To prevent +such situation you have to control the transaction state: + +[source,java] +---- +List txResults = redisOperations.execute(new SessionCallback>() { + public List execute(RedisOperations operations) throws DataAccessException { + boolean transactionStateIsActive = true; + try { + operations.multi(); + operations.opsForSet().add("key", "value1"); + + // This will contain the results of all operations in the transaction + return operations.exec(); + } finally{ + if (transactionStateIsActive) { + LOG.error("Transaction state is active"); + operations.discard(); + } + } + } +}); +---- + [[tx.spring]] == `@Transactional` Support From 7d44460f9f3ac7408efe42240ba86d2957adaedb Mon Sep 17 00:00:00 2001 From: Egor Ponomarev Date: Thu, 14 Mar 2024 05:01:45 -0500 Subject: [PATCH 2/2] Fix a typo --- src/main/antora/modules/ROOT/pages/redis/transactions.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/antora/modules/ROOT/pages/redis/transactions.adoc b/src/main/antora/modules/ROOT/pages/redis/transactions.adoc index f017592836..ee823b9469 100644 --- a/src/main/antora/modules/ROOT/pages/redis/transactions.adoc +++ b/src/main/antora/modules/ROOT/pages/redis/transactions.adoc @@ -25,7 +25,7 @@ System.out.println("Number of items added to set: " + txResults.get(0)); `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. -It worth to mention that if between the commands multi() and exec() a query timeout exception happens (e.g. in case +It is worth mentioning that if between the commands multi() and exec() a query timeout exception happens (e.g. in case Redis is not available to respond fast) then the connection may get stuck in a transactional state. To prevent such situation you have to control the transaction state: