Jedis编程设计:连接池
一.连接池基本参数
- maxActive:链接池中最大连接数,默认为8
- maxIdle:链接池中最大空闲的连接数,默认为8
- minIdle:连接池中最少空闲的连接数,默认为0
- maxWait:当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1,表示永不超时
- minEvictableIdleTimeMillis:连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除
- softMinEvictableIdleTimeMillis:连接空闲的最小时间,达到此值后空闲链接将会被移除,且保留“minIdle”个空闲连接数。默认为-1
- numTestsPerEvictionRun:对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3
- testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值
- testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值
- testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值
- timeBetweenEvictionRunsMillis:“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1
- whenExhaustedAction:当“连接池”中active数量达到阀值时,即“链接”资源耗尽时,连接池需要采取的手段, 默认为1
-> 0 :抛出异常,
-> 1 :阻塞,直到有可用链接资源
-> 2 :强制创建新的链接资源
二.程序实例
public class PoolTestMain {public static void main(String[] args) {JedisPoolConfig config = new JedisPoolConfig();config.setMaxActive(32);config.setMaxIdle(6);config.setMinIdle(0);config.setMaxWait(15000);config.setMinEvictableIdleTimeMillis(300000);config.setSoftMinEvictableIdleTimeMillis(-1);config.setNumTestsPerEvictionRun(3);config.setTestOnBorrow(false);config.setTestOnReturn(false);config.setTestWhileIdle(false);config.setTimeBetweenEvictionRunsMillis(60000);//一分钟config.setWhenExhaustedAction((byte)1);JedisPool pool = new JedisPool(config,"127.0.0.1",6379,15000,"0123456",12);Jedis client = pool.getResource(); //从pool中获取资源try{client.select(0);client.set("k1", "v1");System.out.println(client.get("k1"));}catch(Exception e){e.printStackTrace();}finally{pool.returnResource(client); //向连接池“归还”资源,千万不要忘记。}}}
三.Spring与Jedis连接池
1) beans.xml:位于/resources/beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName"><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxActive" value="32"></property><property name="maxIdle" value="6"></property><property name="maxWait" value="15000"></property><property name="minEvictableIdleTimeMillis" value="300000"></property><property name="numTestsPerEvictionRun" value="3"></property><property name="timeBetweenEvictionRunsMillis" value="60000"></property><property name="whenExhaustedAction" value="1"></property></bean><bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy"><!-- config --><constructor-arg ref="jedisPoolConfig"></constructor-arg><!-- host --><constructor-arg value="127.0.0.1"></constructor-arg><!-- port --><constructor-arg value="6379"></constructor-arg><!-- timeout --><constructor-arg value="15000"></constructor-arg><!-- password --><constructor-arg value="0123456"></constructor-arg><!-- database index --><constructor-arg value="12"></constructor-arg></bean></beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName"> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="32"></property>
<property name="maxIdle" value="6"></property>
<property name="maxWait" value="15000"></property>
<property name="minEvictableIdleTimeMillis" value="300000"></property>
<property name="numTestsPerEvictionRun" value="3"></property>
<property name="timeBetweenEvictionRunsMillis" value="60000"></property>
<property name="whenExhaustedAction" value="1"></property>
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">
<!-- config -->
<constructor-arg ref="jedisPoolConfig"></constructor-arg>
<!-- host -->
<constructor-arg value="127.0.0.1"></constructor-arg>
<!-- port -->
<constructor-arg value="6379"></constructor-arg>
<!-- timeout -->
<constructor-arg value="15000"></constructor-arg>
<!-- password -->
<constructor-arg value="0123456"></constructor-arg>
<!-- database index -->
<constructor-arg value="12"></constructor-arg>
</bean>
</beans>
2) 测试类:不过在实际的spring环境中,只需要“注入”即可.
public static void main(String[] args) {//resources/beans.xmlClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");JedisPool jedisPool = (JedisPool)context.getBean("jedisPool");Jedis client = jedisPool.getResource();try{client.select(0);client.set("k1", "v1");System.out.println(client.get("k1"));}catch(Exception e){e.printStackTrace();}finally{jedisPool.returnResource(client);//must be}}
Jedis编程设计:连接池的更多相关文章
- Jedis分片Sentinel连接池实验
Jedis分片Sentinel连接池实验 1.起因 众所周知,Redis官方HA工具Sentinel已经问世很久了,但令人费解的是,Jedis官方却迟迟没有更新它的连接池.到目前Maven库中最新的2 ...
- 简单是Jedis实例(相对连接池而言)
在引入相关jar包后,只要new一个Jedis对象,就能做redis相关操作了.以下是一个简单的jedis实例: package com.pptv.redis; import java.util.Ar ...
- Jedis连接池对Redis数据操作
[效果图] [前言] Redis是常用于缓存的非关系型数据库,感觉更像加强型的HashMap的用法,依靠Key和Value保存数据.官方推荐用Jedis来操作Redis数据库,使用和JDBC差不多,一 ...
- Java与redis交互、Jedis连接池JedisPool
Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...
- jedis的连接池
1.需要先打开虚拟机,并开启Linux系统的端口号:6379: 其中,第一行代码为修改字符编码格式,解决SSH中文乱码问题. 2.开启redis: 3.利用连接池实现数据的存取: (1)代码实现: i ...
- Linux编程之内存池的设计与实现(C++98)
假设服务器的硬件资源"充裕",那么提高服务器性能的一个很直接的方法就是空间换时间,即"浪费"服务器的硬件资源,以换取其运行效率.提升服务器性能的一个重要方法就是 ...
- jedis连接池详解(Redis)
转自:http://tianxingzhe.blog.51cto.com/3390077/1684306 原子性(atomicity): 一个事务是一个不可分割的最小工作单位,事务中包括的诸操作要么都 ...
- Jedis连接池使用
构建redis连接池,返还到连接池 private static JedisPool jedisPool = null; private static Jedis jedis; static { je ...
- java客户端Jedis操作Redis Sentinel 连接池
pom配置: <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...
随机推荐
- libvirtError: no connection driver available for qemu:///system 解决办法
今天部署了一套K版的OpenStack环境后,启动nova-compute后,日志里狂报如下错误(因为其内部有定时任务需要去连libvirtd进程): 2015-08-14 10:01:00.061 ...
- debug进入线程中
今天debug调试程序时,怎么也进入不了线程中,f5直接进源码,f6直接跳过了,后来把断点打在线程的Run()方法里面,按f8(可能要多按几次)就可以了.
- sql server触发器中增删改判断
触发器生效逻辑 在Before或者After之后使用INSERT,DELETE,UPDATE 触发器内情况判断 插入 if exists(select 1 from inserted) and not ...
- UI学习笔记---第八天
UINavigationController的用法 界面间传值 UInavigationController继承于UIViewController,以栈的方式管理所控制的师徒控制器,至少要有一个被 ...
- 在web.xml中配置error-page
在web.xml中配置error-page 在web.xml中有两种配置error-page的方法,一是通过错误码来配置,而是通过异常的类型来配置,分别举例如下: 一. 通过错误码来配置error ...
- Jlink V7在MDK下使用Cortex-M3-Jlink模式开发STM32的说明
Jlink V7在MDK下使用Cortex-M3-Jlink模式开发STM32的说明 开发环境:STM32F103RB(128K Flash 20K RAM)+MDK3.50+JLINK V7(v ...
- JavaScript个人学习记录总结(二)——验证表单输入之模式匹配
该示例检查从文本窗口部件中获取姓名和电话号码这两个表单数据的有效性.当文本框中的值发生变化时,即引发一个change事件,从而可以调用一个函数来检查这两个输入值的格式是否正确. validator.h ...
- spark_updateStateByKey
java核心代码 JavaPairDStream<String, Integer> wordCounts = pair.updateStateByKey(new Function2< ...
- UI组件(思维导图)
- 越狱Season 1-Episode 15: By the Skin and the Teeth
Season 1, Episode 15: By the Skin and the Teeth -Pope: doctor...you can leave. 医生你得离开 -Burrows: It's ...