From a4663d2a0b3aa98c297b72f51fd43a1a79cca26e 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] Update documentation of transactions page. Closes #2865 Original pull request: #2870 --- .../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 c8b872088..ee823b946 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 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: + +[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