连接池的配置问题
连接池配置是跟着老师的做:
jedis.host = 192.168.0.113
jedis.port = 6379
jedis.pool.maxTotal = 100
jedis.pool.maxIdle = 20
jedis.pool.maxWaitMillis = 3000
jedis.pool.testOnBorrow = true
但是程序总是报错,从下往上看:
先是:Caused by: java.util.NoSuchElementException: Unable to validate object
然后是:redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted
我把老师的配置都注释掉,用连接池的默认配置,程序可以正常运行。
对Jedis的连接池并不怎么了解,百度没找到比较系统的文章或博客,还请老师帮忙看一下。
正在回答 回答被采纳积分+1
这里是
jedis.properties配置:
jedis.host = 192.168.0.113
jedis.port = 6379
jedis.pool.maxTotal = 100
jedis.pool.maxIdle = 20
jedis.pool.maxWaitMillis = 30000
jedis.pool.testOnBorrow = true
这里是
spring_redis.xml配置:
<context:property-placeholder location="classpath:jedis.properties" ignore-unresolvable="true"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--当前这个连接池的连接数最大值-->
<property name="maxTotal" value="${jedis.pool.maxTotal}"/>
<!--当前这个连接池中空闲并随时待命的连接数最大值-->
<property name="maxIdle" value="${jedis.pool.maxIdle}"/>
<!--当前这个连接池 如果没有可用的空闲的连接时,等待连接归还的时间的最大值,超出则抛出异常-->
<property name="maxWaitMillis" value="${jedis.pool.maxWaitMillis}"/>
<!--在获取连接时,连接池会检查当前这个连接是否有效,如果无效则重新获取下一条连接-->
<property name="testOnBorrow" value="${jedis.pool.testOnBorrow}"/>
</bean>
<bean id="jedisPoolUtil" class="com.imooc.o2o.cache.JedisPoolUtil">
<constructor-arg index="0" ref="jedisPoolConfig"/>
<constructor-arg index="1" value="${jedis.host}"/>
<constructor-arg index="2" value="${jedis.port}"/>
</bean>
<bean id="jedisUtil" class="com.imooc.o2o.cache.JedisUtil">
<property name="jedisPool" ref="jedisPoolUtil"/>
</bean>
这里是
java工具类JedisPoolUtil:
public class JedisPoolUtil {
private JedisPool jedisPool;
public JedisPoolUtil() {
}
public JedisPoolUtil(final JedisPoolConfig config, final String host, final int port) {
try {
jedisPool = new JedisPool(config, host, port);
} catch (Exception e) {
throw e;
}
}
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
}
这里是
java工具类JedisUtil:
public class JedisUtil {
private JedisPool jedisPool;
public JedisUtil() {
}
public Jedis getJedis() {
return jedisPool.getResource();
}
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPoolUtil jedisPool) {
this.jedisPool = jedisPool.getJedisPool();
}
}
这里是
Junit测试方法:
@Test
public void jedisTest() {
Jedis jedis = jedisUtil.getJedis();
jedis.auth("553774989");
String pong = jedis.ping();
System.out.println(pong);
jedis.select(0);
jedis.set("pretty", "girl");
String pretty = jedis.get("pretty");
System.out.println(pretty);
}
这里是
异常信息:
redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted
at redis.clients.jedis.util.Pool.getResource(Pool.java:53)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:288)
at com.imooc.o2o.cache.JedisUtil.getJedis(JedisUtil.java:15)
at com.imooc.o2o.cache.JedisTest.jedisTest(JedisTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.util.NoSuchElementException: Unable to validate object
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:486)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:349)
at redis.clients.jedis.util.Pool.getResource(Pool.java:50)
... 33 more
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星