Wrap native Exceptions in LettuceConnectionFactory
DATAREDIS-191 - Wrap native Exceptions in RedisConnectionFailureExceptions - Support switching from shared to non-shared connection by resetting shared connection in afterPropertiesSet
This commit is contained in:
@@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.RedisConnectionFailureException;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -82,6 +83,7 @@ public class LettuceConnectionFactory implements InitializingBean, DisposableBea
|
||||
public void afterPropertiesSet() {
|
||||
client = new RedisClient(hostName, port);
|
||||
client.setDefaultTimeout(timeout, TimeUnit.MILLISECONDS);
|
||||
resetConnection();
|
||||
if (shareNativeConnection) {
|
||||
initConnection();
|
||||
}
|
||||
@@ -105,7 +107,7 @@ public class LettuceConnectionFactory implements InitializingBean, DisposableBea
|
||||
if (this.connection != null) {
|
||||
resetConnection();
|
||||
}
|
||||
this.connection = client.connectAsync(LettuceUtils.CODEC);
|
||||
this.connection = createLettuceConnector();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,7 +284,16 @@ public class LettuceConnectionFactory implements InitializingBean, DisposableBea
|
||||
return this.connection;
|
||||
}
|
||||
} else {
|
||||
return createLettuceConnector();
|
||||
}
|
||||
}
|
||||
|
||||
private RedisAsyncConnection<byte[], byte[]> createLettuceConnector() {
|
||||
try {
|
||||
return client.connectAsync(LettuceUtils.CODEC);
|
||||
} catch (RedisException e) {
|
||||
throw new RedisConnectionFailureException("Unable to connect to Redis on " +
|
||||
getHostName() + ":" + getPort(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertNotSame;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.redis.RedisConnectionFailureException;
|
||||
import org.springframework.data.redis.RedisSystemException;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
|
||||
@@ -169,4 +170,44 @@ public class LettuceConnectionFactoryTests {
|
||||
factory.initConnection();
|
||||
assertNotSame(nativeConn, factory.getConnection().getNativeConnection());
|
||||
}
|
||||
|
||||
@Test(expected=RedisConnectionFailureException.class)
|
||||
public void testInitConnectionException() {
|
||||
factory.setHostName("fakeHost");
|
||||
factory.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test(expected=RedisConnectionFailureException.class)
|
||||
public void testGetConnectionNotSharedException() {
|
||||
factory.setShareNativeConnection(false);
|
||||
factory.setHostName("fakeHost");
|
||||
factory.afterPropertiesSet();
|
||||
factory.getConnection();
|
||||
}
|
||||
|
||||
@Test(expected=RedisConnectionFailureException.class)
|
||||
public void testGetConnectionSharedException() {
|
||||
factory.setShareNativeConnection(false);
|
||||
factory.setHostName("fakeHost");
|
||||
factory.afterPropertiesSet();
|
||||
factory.setShareNativeConnection(true);
|
||||
factory.getConnection();
|
||||
}
|
||||
|
||||
@Test(expected=RedisConnectionFailureException.class)
|
||||
public void testGetNativeConnectionNotSharedException() {
|
||||
factory.setShareNativeConnection(false);
|
||||
factory.setHostName("fakeHost");
|
||||
factory.afterPropertiesSet();
|
||||
factory.getNativeConnection();
|
||||
}
|
||||
|
||||
@Test(expected=RedisConnectionFailureException.class)
|
||||
public void testGetNativeConnectionSharedException() {
|
||||
factory.setShareNativeConnection(false);
|
||||
factory.setHostName("fakeHost");
|
||||
factory.afterPropertiesSet();
|
||||
factory.setShareNativeConnection(true);
|
||||
factory.getNativeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user