redis.clients.jedis.util.Pool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jedis_preview Show documentation
Show all versions of jedis_preview Show documentation
Jedis is a blazingly small and sane Redis java client.
The newest version!
package redis.clients.jedis.util;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.exceptions.JedisException;
public class Pool extends GenericObjectPool {
// Legacy
public Pool(GenericObjectPoolConfig poolConfig, PooledObjectFactory factory) {
this(factory, poolConfig);
}
public Pool(final PooledObjectFactory factory, final GenericObjectPoolConfig poolConfig) {
super(factory, poolConfig);
}
public Pool(final PooledObjectFactory factory) {
super(factory);
}
@Override
public void close() {
destroy();
}
public void destroy() {
try {
super.close();
} catch (RuntimeException e) {
throw new JedisException("Could not destroy the pool", e);
}
}
public T getResource() {
try {
return super.borrowObject();
} catch (JedisException je) {
throw je;
} catch (Exception e) {
throw new JedisException("Could not get a resource from the pool", e);
}
}
public void returnResource(final T resource) {
if (resource == null) {
return;
}
try {
super.returnObject(resource);
} catch (RuntimeException e) {
throw new JedisException("Could not return the resource to the pool", e);
}
}
public void returnBrokenResource(final T resource) {
if (resource == null) {
return;
}
try {
super.invalidateObject(resource);
} catch (Exception e) {
throw new JedisException("Could not return the broken resource to the pool", e);
}
}
@Override
public void addObjects(int count) {
try {
for (int i = 0; i < count; i++) {
addObject();
}
} catch (Exception e) {
throw new JedisException("Error trying to add idle objects", e);
}
}
}