druid 配置教程
java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,有不得不使用数据库连接池。数据库连接池有很多选择,c3p、dhcp、proxool等,druid作为一名后起之秀,凭借其出色的性能,也逐渐印入了大家的眼帘。接下来本教程就说一下druid的简单使用。
?首先从http://repo1.maven.org/maven2/com/alibaba/druid/?下载最新的jar包。如果想使用最新的源码编译,可以从https://github.com/alibaba/druid?下载源码,然后使用maven命令行,或者导入到eclipse中进行编译。
和dbcp类似,druid的配置项如下
?
配置缺省值说明name?配置这个属性的意义在于,如果存在多个数据源,监控的时候表1.1 配置属性
?
根据常用的配置属性,首先给出一个如下的配置文件,放置于src目录下。
url:jdbc:mysql://localhost:3306/dragoon_v25_masterdbdriverClassName:com.mysql.jdbc.Driverusername:rootpassword:aaaaaaaa filters:stat maxActive:20initialSize:1maxWait:60000minIdle:10#maxIdle:15 timeBetweenEvictionRunsMillis:60000minEvictableIdleTimeMillis:300000 validationQuery:SELECT 'x'testWhileIdle:truetestOnBorrow:falsetestOnReturn:false#poolPreparedStatements:truemaxOpenPreparedStatements:20#对于长时间不使用的连接强制关闭removeAbandoned:true#超过30分钟开始关闭空闲连接removeAbandonedTimeout:1800#将当前关闭动作记录到日志logAbandoned:true
?配置文件1.1?
配置项中指定了各个参数后,在连接池内部是这么使用这些参数的。数据库连接池在初始化的时候会创建initialSize个连接,当有数据库操作时,会从池中取出一个连接。如果当前池中正在使用的连接数等于maxActive,则会等待一段时间,等待其他操作释放掉某一个连接,如果这个等待时间超过了maxWait,则会报错;如果当前正在使用的连接数没有达到maxActive,则判断当前是否空闲连接,如果有则直接使用空闲连接,如果没有则新建立一个连接。在连接使用完毕后,不是将其物理连接关闭,而是将其放入池中等待其他操作复用。
同时连接池内部有机制判断,如果当前的总的连接数少于miniIdle,则会建立新的空闲连接,以保证连接数得到miniIdle。如果当前连接池中某个连接在空闲了timeBetweenEvictionRunsMillis时间后任然没有使用,则被物理性的关闭掉。有些数据库连接的时候有超时限制(mysql连接在8小时后断开),或者由于网络中断等原因,连接池的连接会出现失效的情况,这时候设置一个testWhileIdle参数为true,可以保证连接池内部定时检测连接的可用性,不可用的连接会被抛弃或者重建,最大情况的保证从连接池中得到的Connection对象是可用的。当然,为了保证绝对的可用性,你也可以使用testOnBorrow为true(即在获取Connection对象时检测其可用性),不过这样会影响性能。
首先给出spring配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- 给web使用的spring文件 --><bean id="propertyConfigurer"value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /><property name="driverClassName" value="${driverClassName}" /><property name="filters" value="${filters}" /><property name="maxActive" value="${maxActive}" /><property name="initialSize" value="${initialSize}" /><property name="maxWait" value="${maxWait}" /><property name="minIdle" value="${minIdle}" /><property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /><property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /><property name="validationQuery" value="${validationQuery}" /><property name="testWhileIdle" value="${testWhileIdle}" /><property name="testOnBorrow" value="${testOnBorrow}" /><property name="testOnReturn" value="${testOnReturn}" /><property name="maxOpenPreparedStatements"value="${maxOpenPreparedStatements}" /><property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 1800秒,也就是30分钟 --> <property name="logAbandoned" value="${logAbandoned}" /> <!-- 关闭abanded连接时输出错误日志 --></bean><bean id="dataSourceDbcp" value="${driverClassName}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /><property name="maxActive" value="${maxActive}" /><property name="minIdle" value="${minIdle}" /><property name="maxWait" value="${maxWait}" /><property name="defaultAutoCommit" value="true" /><property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /><property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /><property name="validationQuery" value="${validationQuery}" /><property name="testWhileIdle" value="${testWhileIdle}" /><property name="testOnBorrow" value="${testOnBorrow}" /><property name="testOnReturn" value="${testOnReturn}" /><property name="maxOpenPreparedStatements"value="${maxOpenPreparedStatements}" /><property name="removeAbandoned" value="${removeAbandoned}" /> <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <property name="logAbandoned" value="${logAbandoned}" /></bean><!-- jdbcTemplate --><bean id="jdbc" /></property></bean><bean id="SpringTableOperatorBean" /></property></bean></beans>
?
纯java代码配置
?
private static final DruidDataSource cpds = new DruidDataSource(); private static ResourceBundle bundle = null;bundle = ResourceBundle.getBundle("dbconfig", Locale.getDefault()); //基本属性 url、user、password cpds.setUrl(bundle.getString("jdbc.url")); cpds.setUsername(bundle.getString("jdbc.user")); cpds.setPassword(bundle.getString("jdbc.password")); //配置初始化大小、最小、最大 cpds.setInitialSize(1); cpds.setMinIdle(1); cpds.setMaxActive(30); //配置获取连接等待超时的时间 cpds.setMaxWait(60000); //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 cpds.setTimeBetweenEvictionRunsMillis(60000); /**配置一个连接在池中最小生存的时间,单位是毫秒*/ cpds.setMinEvictableIdleTimeMillis(30000); //用来检测连接是否有效的sql,要求是一个查询语句。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用。 cpds.setValidationQuery("SELECT 'x'"); //建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 cpds.setTestWhileIdle(true); //申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 cpds.setTestOnBorrow(false); //归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 cpds.setTestOnReturn(false); //打开PSCache,并且指定每个连接上PSCache的大小 mysql5.5及以上请开启 cpds.setPoolPreparedStatements(true); cpds.setMaxPoolPreparedStatementPerConnectionSize(40); //打开removeAbandoned功能 cpds.setRemoveAbandoned(true); cpds.setRemoveAbandonedTimeout(600); cpds.setLogAbandoned(true); //配置监控统计拦截的filters cpds.setFilters("stat");
?