pool doesn't throw Exception anymore but JedisException

This commit is contained in:
Jonathan Leibiusky
2010-12-10 17:36:44 -03:00
parent dadaaecfdb
commit 6f92f5fc9c
3 changed files with 39 additions and 19 deletions

View File

@@ -3,6 +3,8 @@ package redis.clients.util;
import org.apache.commons.pool.PoolableObjectFactory; import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool; import org.apache.commons.pool.impl.GenericObjectPool;
import redis.clients.jedis.JedisException;
public abstract class Pool<T> { public abstract class Pool<T> {
private final GenericObjectPool internalPool; private final GenericObjectPool internalPool;
@@ -12,19 +14,38 @@ public abstract class Pool<T> {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T getResource() throws Exception { public T getResource() {
return (T) internalPool.borrowObject(); try {
return (T) internalPool.borrowObject();
} catch (Exception e) {
throw new JedisException("Could not get a resource from the pool",
e);
}
} }
public void returnResource(final T resource) throws Exception { public void returnResource(final T resource) {
internalPool.returnObject(resource); try {
internalPool.returnObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
} }
public void returnBrokenResource(final T resource) throws Exception { public void returnBrokenResource(final T resource) {
internalPool.invalidateObject(resource); try {
internalPool.invalidateObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
} }
public void destroy() throws Exception { public void destroy() {
internalPool.close(); try {
internalPool.close();
} catch (Exception e) {
throw new JedisException("Could not destroy the pool", e);
}
} }
} }

View File

@@ -1,13 +1,12 @@
package redis.clients.jedis.tests; package redis.clients.jedis.tests;
import java.util.NoSuchElementException;
import org.apache.commons.pool.impl.GenericObjectPool; import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool.Config; import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisException;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
@@ -66,8 +65,8 @@ public class JedisPoolTest extends Assert {
pool.destroy(); pool.destroy();
} }
@Test(expected = NoSuchElementException.class) @Test(expected = JedisException.class)
public void checkPoolOverflow() throws Exception { public void checkPoolOverflow() {
Config config = new Config(); Config config = new Config();
config.maxActive = 1; config.maxActive = 1;
config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;

View File

@@ -4,7 +4,6 @@ import java.io.IOException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
import org.apache.commons.pool.impl.GenericObjectPool; import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool.Config; import org.apache.commons.pool.impl.GenericObjectPool.Config;
@@ -13,6 +12,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisException;
import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool; import redis.clients.jedis.ShardedJedisPool;
@@ -44,7 +44,7 @@ public class ShardedJedisPoolTest extends Assert {
} }
@Test @Test
public void checkConnections() throws Exception { public void checkConnections() {
ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards); ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
ShardedJedis jedis = pool.getResource(); ShardedJedis jedis = pool.getResource();
jedis.set("foo", "bar"); jedis.set("foo", "bar");
@@ -54,7 +54,7 @@ public class ShardedJedisPoolTest extends Assert {
} }
@Test @Test
public void checkConnectionWithDefaultPort() throws Exception { public void checkConnectionWithDefaultPort() {
ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards); ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
ShardedJedis jedis = pool.getResource(); ShardedJedis jedis = pool.getResource();
jedis.set("foo", "bar"); jedis.set("foo", "bar");
@@ -64,7 +64,7 @@ public class ShardedJedisPoolTest extends Assert {
} }
@Test @Test
public void checkJedisIsReusedWhenReturned() throws Exception { public void checkJedisIsReusedWhenReturned() {
ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards); ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
ShardedJedis jedis = pool.getResource(); ShardedJedis jedis = pool.getResource();
jedis.set("foo", "0"); jedis.set("foo", "0");
@@ -89,8 +89,8 @@ public class ShardedJedisPoolTest extends Assert {
pool.destroy(); pool.destroy();
} }
@Test(expected = NoSuchElementException.class) @Test(expected = JedisException.class)
public void checkPoolOverflow() throws Exception { public void checkPoolOverflow() {
Config config = new Config(); Config config = new Config();
config.maxActive = 1; config.maxActive = 1;
config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
@@ -105,7 +105,7 @@ public class ShardedJedisPoolTest extends Assert {
} }
@Test @Test
public void shouldNotShareInstances() throws Exception { public void shouldNotShareInstances() {
Config config = new Config(); Config config = new Config();
config.maxActive = 2; config.maxActive = 2;
config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;