public abstract class ConnectionPoolSupport extends Object
GenericObjectPool
and SoftReferenceObjectPool
. Connection pool creation requires
a Supplier
that creates Redis connections. The pool can allocate either wrapped or direct connections.
StatefulConnection.close()
.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
BLPOP
.BLPOP
.command batching
.// 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();
Modifier and Type | Method and Description |
---|---|
static <T extends StatefulConnection<?,?>> |
createGenericObjectPool(Supplier<T> connectionSupplier,
org.apache.commons.pool2.impl.GenericObjectPoolConfig<T> config)
Creates a new
GenericObjectPool using the Supplier . |
static <T extends StatefulConnection<?,?>> |
createGenericObjectPool(Supplier<T> connectionSupplier,
org.apache.commons.pool2.impl.GenericObjectPoolConfig<T> config,
boolean wrapConnections)
Creates a new
GenericObjectPool using the Supplier . |
static <T extends StatefulConnection<?,?>> |
createSoftReferenceObjectPool(Supplier<T> connectionSupplier)
Creates a new
SoftReferenceObjectPool using the Supplier . |
static <T extends StatefulConnection<?,?>> |
createSoftReferenceObjectPool(Supplier<T> connectionSupplier,
boolean wrapConnections)
Creates a new
SoftReferenceObjectPool using the Supplier . |
public static <T extends StatefulConnection<?,?>> org.apache.commons.pool2.impl.GenericObjectPool<T> createGenericObjectPool(Supplier<T> connectionSupplier, org.apache.commons.pool2.impl.GenericObjectPoolConfig<T> config)
GenericObjectPool
using the Supplier
. Allocated instances are wrapped and must not be
returned with ObjectPool.returnObject(Object)
.T
- connection type.connectionSupplier
- must not be null
.config
- must not be null
.public static <T extends StatefulConnection<?,?>> org.apache.commons.pool2.impl.GenericObjectPool<T> createGenericObjectPool(Supplier<T> connectionSupplier, org.apache.commons.pool2.impl.GenericObjectPoolConfig<T> config, boolean wrapConnections)
GenericObjectPool
using the Supplier
.T
- connection type.connectionSupplier
- must not be null
.config
- must not be null
.wrapConnections
- false
to return direct connections that need to be returned to the pool using
ObjectPool.returnObject(Object)
. true
to return wrapped connection that are returned to the
pool when invoking StatefulConnection.close()
.public static <T extends StatefulConnection<?,?>> org.apache.commons.pool2.impl.SoftReferenceObjectPool<T> createSoftReferenceObjectPool(Supplier<T> connectionSupplier)
SoftReferenceObjectPool
using the Supplier
. Allocated instances are wrapped and must not be
returned with ObjectPool.returnObject(Object)
.T
- connection type.connectionSupplier
- must not be null
.public static <T extends StatefulConnection<?,?>> org.apache.commons.pool2.impl.SoftReferenceObjectPool<T> createSoftReferenceObjectPool(Supplier<T> connectionSupplier, boolean wrapConnections)
SoftReferenceObjectPool
using the Supplier
.T
- connection type.connectionSupplier
- must not be null
.wrapConnections
- false
to return direct connections that need to be returned to the pool using
ObjectPool.returnObject(Object)
. true
to return wrapped connection that are returned to the
pool when invoking StatefulConnection.close()
.Copyright © 2023 lettuce.io. All rights reserved.