Skip to content

Commit 91affcb

Browse files
jxblummp911de
authored andcommitted
Edit ref docs and Javadoc for connection classes to clearly state the Thread-safety guarantees.
Closes #2653 Original pull request: #2667
1 parent 49abd4e commit 91affcb

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

Diff for: src/main/asciidoc/reference/redis.adoc

+5-1
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,16 @@ One of the first tasks when using Redis and Spring is to connect to the store th
7474
[[redis:connectors:connection]]
7575
=== RedisConnection and RedisConnectionFactory
7676

77-
`RedisConnection` provides the core building block for Redis communication, as it handles the communication with the Redis back end. It also automatically translates the underlying connecting library exceptions to Spring's consistent DAO exception {spring-framework-reference}/data-access.html#dao-exceptions[hierarchy] so that you can switch the connectors without any code changes, as the operation semantics remain the same.
77+
`RedisConnection` provides the core building block for Redis communication, as it handles the communication with the Redis backend. It also automatically translates underlying connecting library exceptions to Spring's consistent {spring-framework-reference}/data-access.html#dao-exceptions[DAO exception hierarchy] so that you can switch connectors without any code changes, as the operation semantics remain the same.
7878

7979
NOTE: For the corner cases where the native library API is required, `RedisConnection` provides a dedicated method (`getNativeConnection`) that returns the raw, underlying object used for communication.
8080

8181
Active `RedisConnection` objects are created through `RedisConnectionFactory`. In addition, the factory acts as `PersistenceExceptionTranslator` objects, meaning that, once declared, they let you do transparent exception translation. For example, you can do exception translation through the use of the `@Repository` annotation and AOP. For more information, see the dedicated {spring-framework-reference}/data-access.html#orm-exception-translation[section] in the Spring Framework documentation.
8282

83+
WARNING: `RedisConnection` classes, such as `JedisConnection` and `LettuceConnection`, are **not** Thread-safe. While the underlying native connection, such as Lettuce's `StatefulRedisConnection`, may be Thread-safe, Spring Data Redis's `LettuceConnection` class itself is not Thread-safe. Therefore, you should **not** share instances of a `RedisConnection` across multiple Threads. This is especially true for transactional, or blocking Redis operations and commands, such as `BLPOP`. In transactional and pipelining operations, for instance, `RedisConnection` holds onto unguarded mutable state to complete the operation correctly, thereby making it unsafe to use with multiple Threads. This is by design.
84+
85+
TIP: If you need to share (stateful) Redis resources, like connections, across multiple Threads, for performance reasons or otherwise, then you should acquire the native connection and use the Redis client library (driver) API directly. Alternatively, you can use the `RedisTemplate`, which acquires and manages connections for operations (and Redis commands) in a Thread-safe manner. See <<redis:template,documentation>> on `RedisTemplate` for more details.
86+
8387
NOTE: Depending on the underlying configuration, the factory can return a new connection or an existing connection (when a pool or shared native connection is used).
8488

8589
The easiest way to work with a `RedisConnectionFactory` is to configure the appropriate connector through the IoC container and inject it into the using class.

Diff for: src/main/java/org/springframework/data/redis/connection/RedisConnection.java

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
* A connection to a Redis server. Acts as an common abstraction across various Redis client libraries (or drivers).
2424
* Additionally performs exception translation between the underlying Redis client library and Spring DAO exceptions.
2525
* The methods follow as much as possible the Redis names and conventions.
26+
* <p>
27+
* Spring Data Redis {@link RedisConnection connections}, unlike perhaps their underlying native connection (for example:
28+
* the Lettuce {@literal StatefulRedisConnection}) are not Thread-safe. Please refer to the corresponding the Javadoc
29+
* for Redis client library (driver) specific connections provided by Spring Data Redis for more details.
2630
*
2731
* @author Costin Leau
2832
* @author Christoph Strobl

Diff for: src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@
5959

6060
/**
6161
* {@code RedisConnection} implementation on top of <a href="https://github.com/redis/jedis">Jedis</a> library.
62+
* <p>
63+
* WARNING: The {@link JedisConnection} class is not Thread-safe. This class requires and uses a
64+
* {@literal Jedis} instance from the Jedis client library (driver), which is very clearly
65+
* <a href="https://github.com/redis/jedis/wiki/Getting-started#using-jedis-in-a-multithreaded-environment">documented</a>
66+
* as not Thread-safe.
6267
*
6368
* @author Costin Leau
6469
* @author Jennifer Hickey
@@ -72,6 +77,8 @@
7277
* @author Ninad Divadkar
7378
* @author Guy Korland
7479
* @author Dengliming
80+
* @author John Blum
81+
* @see redis.clients.jedis.Jedis
7582
*/
7683
public class JedisConnection extends AbstractRedisConnection {
7784

Diff for: src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@
7979
/**
8080
* {@code RedisConnection} implementation on top of <a href="https://github.com/mp911de/lettuce">Lettuce</a> Redis
8181
* client.
82+
* <p>
83+
* WARNING: While the underlying Lettuce {@literal RedisClient} and {@literal StatefulRedisConnection} instances used by
84+
* {@link LettuceConnection} are Thread-safe, this class itself is not Thread-safe. Therefore, instances of {@link LettuceConnection}
85+
* should not be shared across multiple Threads when executing Redis commands and other operations. If optimal performance
86+
* is required by your application(s), then we recommend direct access to the low-level, API provided by the underlying
87+
* Lettuce client library (driver), where such Thread-safety guarantees can be made. Simply call {@link #getNativeConnection()}
88+
* and use the native resource as required.
8289
*
8390
* @author Costin Leau
8491
* @author Jennifer Hickey
@@ -89,6 +96,7 @@
8996
* @author Ninad Divadkar
9097
* @author Tamil Selvan
9198
* @author ihaohong
99+
* @author John Blum
92100
*/
93101
public class LettuceConnection extends AbstractRedisConnection {
94102

0 commit comments

Comments
 (0)