Cannot get Jedis connection; nested exception is java.lang.NullPointerException
2017-08-04 16:27
1466 查看
前提:Redis服务器已经运行,且端口号,服务器地址都已经配置正常,但任然抛出无法获取连接异常
原来的代码如下:
[java]
view plain
copy
@Bean
public JedisConnectionFactory connectionFactory(){
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setUsePool(true);
jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
jedisConnectionFactory.setHostName(environment.getProperty("redis.host"));
jedisConnectionFactory.setPort(Integer.parseInt(environment.getProperty("redis.port")));
return jedisConnectionFactory;
}
其他配置都很正常,但是运行后会抛出空指针异常。
解决方法如下,在return前添加如下代码
[java]
view plain
copy
jedisConnectionFactory.afterPropertiesSet();
看源码便知原因:看jedis如何获取到连接的:
[java]
view plain
copy
public JedisConnection getConnection() {
Jedis jedis = fetchJedisConnector();
JedisConnection connection = (usePool ? new JedisConnection(jedis, pool, dbIndex) : new JedisConnection(jedis,
null, dbIndex));
connection.setConvertPipelineAndTxResults(convertPipelineAndTxResults);
return postProcessConnection(connection);
}
我们看fetchJedisConnector()方法:
[java]
view plain
copy
protected Jedis fetchJedisConnector() {
try {
if (usePool && pool != null) {
return pool.getResource();
}
Jedis jedis = new Jedis(getShardInfo());
// force initialization (see Jedis issue #82)
jedis.connect();
return jedis;
} catch (Exception ex) {
throw new RedisConnectionFailureException("Cannot get Jedis connection", ex);
}
}
如果没用用pool的话,需要用到getShardInfo()方法,而这个方法就是返回一个JedisShardInfo shardInfo。那么这个JedisShardInfo shardInfo在什么时候初始化呢?查看后发现,哈哈在这:
[java]
view plain
copy
public void afterPropertiesSet() {
if (shardInfo == null) {
shardInfo = new JedisShardInfo(hostName, port);
if (StringUtils.hasLength(password)) {
shardInfo.setPassword(password);
}
if (timeout > 0) {
setTimeoutOn(shardInfo, timeout);
}
}
if (usePool) {
this.pool = createPool();
}
}
这个方法将在所有的属性被初始化后调用。但是会在init前调用。是spring中InitializingBean接口的方法。spring-data-redis里面实现了它。在这里对shardIndo类进行了初始化。所以,我们只要在代码中添加:
[java]
view plain
copy
jedisConnectionFactory.afterPropertiesSet();
这句就可以啦。再运行一次,连接成功!
原来的代码如下:
[java]
view plain
copy
@Bean
public JedisConnectionFactory connectionFactory(){
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setUsePool(true);
jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
jedisConnectionFactory.setHostName(environment.getProperty("redis.host"));
jedisConnectionFactory.setPort(Integer.parseInt(environment.getProperty("redis.port")));
return jedisConnectionFactory;
}
其他配置都很正常,但是运行后会抛出空指针异常。
Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is java.lang.NullPointerException
解决方法如下,在return前添加如下代码
[java]
view plain
copy
jedisConnectionFactory.afterPropertiesSet();
看源码便知原因:看jedis如何获取到连接的:
[java]
view plain
copy
public JedisConnection getConnection() {
Jedis jedis = fetchJedisConnector();
JedisConnection connection = (usePool ? new JedisConnection(jedis, pool, dbIndex) : new JedisConnection(jedis,
null, dbIndex));
connection.setConvertPipelineAndTxResults(convertPipelineAndTxResults);
return postProcessConnection(connection);
}
我们看fetchJedisConnector()方法:
[java]
view plain
copy
protected Jedis fetchJedisConnector() {
try {
if (usePool && pool != null) {
return pool.getResource();
}
Jedis jedis = new Jedis(getShardInfo());
// force initialization (see Jedis issue #82)
jedis.connect();
return jedis;
} catch (Exception ex) {
throw new RedisConnectionFailureException("Cannot get Jedis connection", ex);
}
}
如果没用用pool的话,需要用到getShardInfo()方法,而这个方法就是返回一个JedisShardInfo shardInfo。那么这个JedisShardInfo shardInfo在什么时候初始化呢?查看后发现,哈哈在这:
[java]
view plain
copy
public void afterPropertiesSet() {
if (shardInfo == null) {
shardInfo = new JedisShardInfo(hostName, port);
if (StringUtils.hasLength(password)) {
shardInfo.setPassword(password);
}
if (timeout > 0) {
setTimeoutOn(shardInfo, timeout);
}
}
if (usePool) {
this.pool = createPool();
}
}
这个方法将在所有的属性被初始化后调用。但是会在init前调用。是spring中InitializingBean接口的方法。spring-data-redis里面实现了它。在这里对shardIndo类进行了初始化。所以,我们只要在代码中添加:
[java]
view plain
copy
jedisConnectionFactory.afterPropertiesSet();
这句就可以啦。再运行一次,连接成功!
相关文章推荐
- spring-data-redis用配置类连接时,抛异常Cannot get Jedis connection; nested exception is java.lang.NullPointerEx
- spring-data-redis用配置类连接时,抛异常Cannot get Jedis connection; nested exception is java.lang.NullPointerE
- Unknown jedis exception; nested exception is java.lang.NullPointerException
- ?--Porg.springframework.beans.MethodInvocationException: Property 'username' threw exception; nested exception is java.lang.NullPointerException
- nested exception is java.lang.NullPointerException
- HTTP Status 500 - Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.Je
- Request processing failed; nested exception is java.lang.NullPointerException
- Request processing failed; nested exception is java.lang.NullPointerException] with root cause
- HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException
- HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException
- threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with
- error:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
- HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException
- Request processing failed; nested exception is java.lang.NullPointerException] with root cause
- HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException
- ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: An attempt by a client to chec
- nested exception is java.lang.NullPointerException
- Could not open JPA EntityManager for transaction; nested exception is java.lang.NullPointerException
- Request processing failed; nested exception is java.lang.NullPointerException
- HTTP Status 500 - Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.J