错误说明:.
使用Spring、MyBatis、mybatis-redis(含Cache实现) 整合Redis做二级缓存时,启动测试类,所报的错
错误如下(只留下关键部分):.
从以下错误日志信息,简单可以知道,①Redis连接超时,②连接不上本地主机:6379(关键,我使用的是远程的Redis,不是本地),③不能获取连接池…
=======================start===========================
查询所有的书籍:
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@73ff4fae] was not registered for synchronization because synchronization is not active
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@73ff4fae]
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
### Cause: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
### Cause: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host localhost:6379
Caused by: java.net.SocketTimeoutException: connect timed out
Process finished with exit code -1
解决流程:.
①首先,对Redis连接超时问题(全程网络连接正常),进行分析
连接超时,多半是我配置文件时间,超时设置少了;但是我的配置文件中,超时时间设置为10s,所以不可能是时间设置问题
写一个测试类,去测试是否能够连接Redis;测试结果是可以连接到Redis,连接上没问题
前两个都不是,暂时也就只能看 ② 连接不上本地主机:6379;因为我的Redis是在服务器上搭建的,问题应该就是在RedisCache使用的配置信息上,废话不多说,直接断点调试;最后发现问题出在,redis.properties中key的命名上,必须保证其命名与 RedisConfig类属性命名一致,否则返回一个默认的Redis配置信息用于连接Redis服务
下面是调试过程,最后断点位置发现 metaCache.hasSetter(name)
始终返回的是false,导致无法将redis.properties文件内容,写入到jedisConfig这个对象
RedisCache类构造器(解析配置信息parseConfiguration)
设置配置属性
断点调试(最终问题出处)
redis.properties文件内容中key,与RedisConfig属性不符合,导致直接跳过下面第5行代码块
RedisConfig属性截图:
解决方案:.
需保证redis.properties文件中的key与RedisConfig类的属性名对应,即配置文件修改如下
#redis的服务器地址
host=...
#redis的服务端口
port=6379
#链接数据库
database=0
#redis连接密码
password=...
#客户端超时时间单位是毫秒
connectionTimeout=10000
#最大连接数
maxTotal=100
#最大空闲数
maxIdle=20
#最小空闲数
minIdle=1
#最大建立连接等待时间
maxWaitMills=2000
#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
testOnBorrow=true
#当调用return Object方法时,进行有效性检查
testOnReturn=true