java.lang.Object
io.lettuce.core.support.ConnectionPoolSupport
public abstract class ConnectionPoolSupport extends Object
Connection pool support for
GenericObjectPool and SoftReferenceObjectPool. Connection pool creation requires
a Supplier that creates Redis connections. The pool can allocate either wrapped or direct connections.
- Wrapped instances will return the connection back to the pool when called
StatefulConnection.close(). - Regular connections need to be returned to the pool with
GenericObjectPool.returnObject(Object)
Lettuce connections are designed to be thread-safe so one connection can be shared amongst multiple threads and Lettuce
connections auto-reconnect by default. Connection pooling with Lettuce can be
required when you're invoking Redis operations in multiple threads and you use
- blocking commands such as
BLPOP. - transactions
BLPOP. command batching.
Example
// application initialization
RedisClusterClient clusterClient = RedisClusterClient.create(RedisURI.create(host, port));
GenericObjectPool<StatefulRedisClusterConnection<String, String>> pool = ConnectionPoolSupport
.createGenericObjectPool(() -> clusterClient.connect(), new GenericObjectPoolConfig());
// executing work
try (StatefulRedisClusterConnection<String, String> connection = pool.borrowObject()) {
// perform some work
}
// terminating
pool.close();
clusterClient.shutdown();
- Since:
- 4.3
- Author:
- Mark Paluch
-
Method Summary
Modifier and Type Method Description static <T extends StatefulConnection<?, ?>>
GenericObjectPool<T>createGenericObjectPool(Supplier<T> connectionSupplier, GenericObjectPoolConfig<T> config)Creates a newGenericObjectPoolusing theSupplier.static <T extends StatefulConnection<?, ?>>
GenericObjectPool<T>createGenericObjectPool(Supplier<T> connectionSupplier, GenericObjectPoolConfig<T> config, boolean wrapConnections)Creates a newGenericObjectPoolusing theSupplier.static <T extends StatefulConnection<?, ?>>
SoftReferenceObjectPool<T>createSoftReferenceObjectPool(Supplier<T> connectionSupplier)Creates a newSoftReferenceObjectPoolusing theSupplier.static <T extends StatefulConnection<?, ?>>
SoftReferenceObjectPool<T>createSoftReferenceObjectPool(Supplier<T> connectionSupplier, boolean wrapConnections)Creates a newSoftReferenceObjectPoolusing theSupplier.
-
Method Details
-
createGenericObjectPool
public static <T extends StatefulConnection<?, ?>> GenericObjectPool<T> createGenericObjectPool(Supplier<T> connectionSupplier, GenericObjectPoolConfig<T> config)Creates a newGenericObjectPoolusing theSupplier. Allocated instances are wrapped and must not be returned withObjectPool.returnObject(Object).- Type Parameters:
T- connection type.- Parameters:
connectionSupplier- must not benull.config- must not benull.- Returns:
- the connection pool.
-
createGenericObjectPool
public static <T extends StatefulConnection<?, ?>> GenericObjectPool<T> createGenericObjectPool(Supplier<T> connectionSupplier, GenericObjectPoolConfig<T> config, boolean wrapConnections)Creates a newGenericObjectPoolusing theSupplier.- Type Parameters:
T- connection type.- Parameters:
connectionSupplier- must not benull.config- must not benull.wrapConnections-falseto return direct connections that need to be returned to the pool usingObjectPool.returnObject(Object).trueto return wrapped connection that are returned to the pool when invokingStatefulConnection.close().- Returns:
- the connection pool.
-
createSoftReferenceObjectPool
public static <T extends StatefulConnection<?, ?>> SoftReferenceObjectPool<T> createSoftReferenceObjectPool(Supplier<T> connectionSupplier)Creates a newSoftReferenceObjectPoolusing theSupplier. Allocated instances are wrapped and must not be returned withObjectPool.returnObject(Object).- Type Parameters:
T- connection type.- Parameters:
connectionSupplier- must not benull.- Returns:
- the connection pool.
-
createSoftReferenceObjectPool
public static <T extends StatefulConnection<?, ?>> SoftReferenceObjectPool<T> createSoftReferenceObjectPool(Supplier<T> connectionSupplier, boolean wrapConnections)Creates a newSoftReferenceObjectPoolusing theSupplier.- Type Parameters:
T- connection type.- Parameters:
connectionSupplier- must not benull.wrapConnections-falseto return direct connections that need to be returned to the pool usingObjectPool.returnObject(Object).trueto return wrapped connection that are returned to the pool when invokingStatefulConnection.close().- Returns:
- the connection pool.
-