Package io.lettuce.core.support
Class ConnectionPoolSupport
- java.lang.Object
-
- io.lettuce.core.support.ConnectionPoolSupport
-
public abstract class ConnectionPoolSupport extends Object
Connection pool support forGenericObjectPool
andSoftReferenceObjectPool
. Connection pool creation requires aSupplier
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
- Wrapped instances will return the connection back to the pool when called
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends StatefulConnection<?,?>>
GenericObjectPool<T>createGenericObjectPool(Supplier<T> connectionSupplier, GenericObjectPoolConfig<T> config)
Creates a newGenericObjectPool
using theSupplier
.static <T extends StatefulConnection<?,?>>
GenericObjectPool<T>createGenericObjectPool(Supplier<T> connectionSupplier, GenericObjectPoolConfig<T> config, boolean wrapConnections)
Creates a newGenericObjectPool
using theSupplier
.static <T extends StatefulConnection<?,?>>
SoftReferenceObjectPool<T>createSoftReferenceObjectPool(Supplier<T> connectionSupplier)
Creates a newSoftReferenceObjectPool
using theSupplier
.static <T extends StatefulConnection<?,?>>
SoftReferenceObjectPool<T>createSoftReferenceObjectPool(Supplier<T> connectionSupplier, boolean wrapConnections)
Creates a newSoftReferenceObjectPool
using theSupplier
.
-
-
-
Method Detail
-
createGenericObjectPool
public static <T extends StatefulConnection<?,?>> GenericObjectPool<T> createGenericObjectPool(Supplier<T> connectionSupplier, GenericObjectPoolConfig<T> config)
Creates a newGenericObjectPool
using 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 newGenericObjectPool
using theSupplier
.- Type Parameters:
T
- connection type.- Parameters:
connectionSupplier
- must not benull
.config
- must not benull
.wrapConnections
-false
to return direct connections that need to be returned to the pool usingObjectPool.returnObject(Object)
.true
to 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 newSoftReferenceObjectPool
using 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 newSoftReferenceObjectPool
using theSupplier
.- Type Parameters:
T
- connection type.- Parameters:
connectionSupplier
- must not benull
.wrapConnections
-false
to return direct connections that need to be returned to the pool usingObjectPool.returnObject(Object)
.true
to return wrapped connection that are returned to the pool when invokingStatefulConnection.close()
.- Returns:
- the connection pool.
-
-