From 1132fb58473f26a8e8e6099c1acb1653e618b9ed Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Mon, 31 Jan 2011 13:23:01 +0200 Subject: [PATCH] + update to latest Jedis version (add support for rich exceptions) + add handling of broken connections --- .../redis/connection/RedisConnection.java | 8 ++++---- .../connection/jedis/JedisConnection.java | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisConnection.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisConnection.java index a43da80c2..6f9f0a2fd 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisConnection.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisConnection.java @@ -18,7 +18,7 @@ package org.springframework.data.keyvalue.redis.connection; import java.util.List; -import org.springframework.data.keyvalue.redis.UncategorizedRedisException; +import org.springframework.dao.DataAccessException; /** * A connection to a Redis server. Acts as an common abstraction across various @@ -33,10 +33,10 @@ public interface RedisConnection extends RedisCommands { /** * Closes (or quits) the connection. - * - * @throws UncategorizedRedisException in case of exceptions + * + * @throws DataAccessException */ - void close() throws UncategorizedRedisException; + void close() throws DataAccessException; /** * Indicates whether the underlying connection is closed or not. diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnection.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnection.java index c0acd7bbc..a0f84e115 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnection.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnection.java @@ -28,7 +28,6 @@ import java.util.Set; import org.springframework.dao.DataAccessException; import org.springframework.data.keyvalue.UncategorizedKeyvalueStoreException; import org.springframework.data.keyvalue.redis.SubscribedRedisConnectionException; -import org.springframework.data.keyvalue.redis.UncategorizedRedisException; import org.springframework.data.keyvalue.redis.connection.DataType; import org.springframework.data.keyvalue.redis.connection.MessageListener; import org.springframework.data.keyvalue.redis.connection.RedisConnection; @@ -45,6 +44,7 @@ import redis.clients.jedis.Pipeline; import redis.clients.jedis.SortingParams; import redis.clients.jedis.Transaction; import redis.clients.jedis.ZParams; +import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.util.Pool; @@ -66,7 +66,8 @@ public class JedisConnection implements RedisConnection { private final Client client; private final BinaryTransaction transaction; private final Pool pool; - + /** flag indicating whether the connection needs to be dropped or not */ + private boolean broken = false; private volatile JedisSubscription subscription; private volatile Pipeline pipeline; @@ -98,6 +99,10 @@ public class JedisConnection implements RedisConnection { protected DataAccessException convertJedisAccessException(Exception ex) { if (ex instanceof JedisException) { + // check connection flag + if (ex instanceof JedisConnectionException) { + broken = true; + } return JedisUtils.convertJedisAccessException((JedisException) ex); } if (ex instanceof IOException) { @@ -108,11 +113,16 @@ public class JedisConnection implements RedisConnection { } @Override - public void close() throws UncategorizedRedisException { + public void close() throws DataAccessException { // return the connection to the pool try { if (pool != null) { - pool.returnResource(jedis); + if (broken) { + pool.returnBrokenResource(jedis); + } + else { + pool.returnResource(jedis); + } } } catch (Exception ex) { pool.returnBrokenResource(jedis);