Skip to content

Commit 4cf08c7

Browse files
egor-ponomarevmp911de
authored andcommitted
Update documentation of transactions page.
Closes #2865 Original pull request: #2870
1 parent bfb46e0 commit 4cf08c7

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: src/main/antora/modules/ROOT/pages/redis/transactions.adoc

+25
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ System.out.println("Number of items added to set: " + txResults.get(0));
2525
`RedisTemplate` uses its value, hash key, and hash value serializers to deserialize all results of `exec` before returning.
2626
There is an additional `exec` method that lets you pass a custom serializer for transaction results.
2727

28+
It is worth mentioning that if between the commands multi() and exec() a query timeout exception happens (e.g. in case
29+
Redis is not available to respond fast) then the connection may get stuck in a transactional state. To prevent
30+
such situation you have to control the transaction state:
31+
32+
[source,java]
33+
----
34+
List<Object> txResults = redisOperations.execute(new SessionCallback<List<Object>>() {
35+
public List<Object> execute(RedisOperations operations) throws DataAccessException {
36+
boolean transactionStateIsActive = true;
37+
try {
38+
operations.multi();
39+
operations.opsForSet().add("key", "value1");
40+
41+
// This will contain the results of all operations in the transaction
42+
return operations.exec();
43+
} finally{
44+
if (transactionStateIsActive) {
45+
LOG.error("Transaction state is active");
46+
operations.discard();
47+
}
48+
}
49+
}
50+
});
51+
----
52+
2853
[[tx.spring]]
2954
== `@Transactional` Support
3055

0 commit comments

Comments
 (0)