BasicDataSource的池配置属性

  BasicDataSource的池配置属性

池配置属性

指定的内容

initialSize 池启动时创建的连接数量
maxActive 同一时间可以从池分配的最多连接数量。设置为0时表示无限制。
maxIdle 池里不会被释放的最多空闲连接数量。设置为0时表示无限制。
maxOpenPreparedStatements 同一时间能够从语句池里分配的已备语句的最大数量。设置为0时表示无限制。
maxWait 在抛出异常之前,池等待连接被回收的最长时间(当没有可用连接时)。设置为-1表示无限等待。
minEvictableIdleTimeMillis 连接保持空闲而不被驱逐的最长时间。
minIdle 在不新建连接的条件下,池中保持空闲的最少连接数。
poolPreparedStatements 是否对已备语句进行池管理(布尔值)。

 

jdbc.properties文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/vbox?rewriteBatchedStatements=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
#<!--initialSize: 初始化连接-->  
jdbc.initialSize=10
#<!--maxActive: 最大连接数量-->  
jdbc.maxActive=500
tcp.PORT=28577
#<!-- 设置多少毫秒的间隔-->
baidu.pinglv=5
#<!-- 初始化连接 -->
dataSource.initialSize=10
#<!-- 最大空闲连接 -->
dataSource.maxIdle=20
#<!-- 最小空闲连接 -->
dataSource.minIdle=5
#<!--最大连接数量 -->
dataSource.maxActive=50
#<!--是否在自动回收超时连接的时候打印连接的超时错误 -->
dataSource.logAbandoned=true
#<!--是否自动回收超时连接 -->
dataSource.removeAbandoned=true
#<!--超时时间(以秒数为单位) -->
dataSource.removeAbandonedTimeout=180
#<!--超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
dataSource.maxWait=1000
#<!--取得连接时是否进行有效性验证(即是否还和数据库连通的)-->
dataSource.testOnBorrow=true
#<!--返回连接时是否进行有效性验证(即是否还和数据库连通的)-->
dataSource.testOnReturn=true
#<!--连接空闲时是否进行有效性验证(即是否还和数据库连通的)-->
dataSource.testWhileIdle=true
#<!--他们两个配合,可以持续更新连接池中的连接对象,当timeBetweenEvictionRunsMillis 大于0时,-->
#<!--每过timeBetweenEvictionRunsMillis 时间,就会启动一个线程,校验连接池中闲置时间超过minEvictableIdleTimeMillis的连接对象。-->
dataSource.timeBetweenEvictionRunsMillis=3
dataSource.minEvictableIdleTimeMillis=12
java类的书写:
/**
 * HttpClient练习
 * studyHttpClient
 * cn.yuchao.httpclient
 * BasicDataSourceDemo.java
 * 创建人:yuchao
 * 时间:2015-11-6-上午09:56:03 
 * 2015-版权个人所有
 */
package cn.yuchao.httpclient;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * BasicDataSourceDemo
 * 创建人:yuchao
 * 时间:2015-11-6-上午09:56:03 
 * @version 1.0.0
 * 
 */
public class BasicDataSourceDemo {
    
    private static final Logger log  = LoggerFactory.getLogger(BasicDataSourceDemo.class);
    
    public static void main(String[] args) {
         
          Properties dbProps =new Properties();
          Connection connection =null;
            try {
                 dbProps.load(BasicDataSourceDemo.class.getResourceAsStream("/jdbc.properties"));
                 System.setProperty("javax.xml.parsers.DocumentBuilderFactory","com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); 
                 String driverClassName = dbProps.getProperty("jdbc.driverClassName");
                 String url = dbProps.getProperty("jdbc.url");
                 String username = dbProps.getProperty("jdbc.username");
                 String password = dbProps.getProperty("jdbc.password");
                 
                 String initialSize = dbProps.getProperty("dataSource.initialSize");
                 String minIdle = dbProps.getProperty("dataSource.minIdle");
                 String maxIdle = dbProps.getProperty("dataSource.maxIdle");
                 String maxWait = dbProps.getProperty("dataSource.maxWait");
                 String maxActive = dbProps.getProperty("dataSource.maxActive"); 
                 String timeBetweenEvictionRunsMillis =dbProps.getProperty("dataSource.timeBetweenEvictionRunsMillis");
                 String minEvictableIdleTimeMillis =dbProps.getProperty("dataSource.minEvictableIdleTimeMillis");
                 
                 //取得连接时是否进行有效性验证(即是否还和数据库连通的)
                 boolean testOnBorrow = Boolean.valueOf(dbProps.getProperty("dataSource.testOnBorrow","true")).booleanValue();
                 //返回连接时是否进行有效性验证(即是否还和数据库连通的)
                 boolean testOnReturn = Boolean.valueOf(dbProps.getProperty("dataSource.testOnReturn","true")).booleanValue();
                 //连接空闲时是否进行有效性验证(即是否还和数据库连通的)
                 boolean testWhileIdle = Boolean.valueOf(dbProps.getProperty("dataSource.testWhileIdle","true")).booleanValue();
                 //是否在自动回收超时连接的时候打印连接的超时错误
                 boolean logAbandoned = Boolean.valueOf(dbProps.getProperty("dataSource.logAbandoned","false")).booleanValue();
                //是否自动回收超时连接
                 boolean removeAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.removeAbandoned","false"))).booleanValue();
                 //超时时间(以秒数为单位)
                 int removeAbandonedTimeout = Integer.parseInt(dbProps.getProperty("dataSource.removeAbandonedTimeout","300"));
                
                 BasicDataSource dataSource = new BasicDataSource();
                 dataSource.setDriverClassName(driverClassName);
                 dataSource.setUrl(url);
                 dataSource.setUsername(username);
                 dataSource.setPassword(password);
                 
                 //初始化连接数
                 if (StringUtils.isNotBlank(initialSize)) {
                    dataSource.setInitialSize(Integer.parseInt(initialSize));
                }
                //最小空闲连接数
                if (StringUtils.isNotBlank(minIdle)) {
                    dataSource.setMinIdle(Integer.parseInt(minIdle));
                }
                 //最大空闲连接数
                if (StringUtils.isNotBlank(maxIdle)) {
                    dataSource.setMinIdle(Integer.parseInt(maxIdle));
                }
                //超时回收时间(以毫秒为单位)
                if (StringUtils.isNotBlank(maxWait)) {
                    dataSource.setMinIdle(Integer.parseInt(minIdle));
                }
                //最大连接数
                if (StringUtils.isNotBlank(maxActive)) {
                    dataSource.setMinIdle(Integer.parseInt(minIdle));
                }
                
                if (StringUtils.isNotBlank(minEvictableIdleTimeMillis)) {
                    
                }
                //是否在自动回收超时连接的时候打印连接的超时错误
                dataSource.setLogAbandoned(logAbandoned);
                //是否自动回收超时连接
                dataSource.setRemoveAbandoned(removeAbandoned);
                //超时时间(以秒数为单位)
                dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
                //取得连接时是否进行有效性验证(即是否还和数据库连通的)
                dataSource.setTestOnBorrow(testOnBorrow);
                //返回连接时是否进行有效性验证(即是否还和数据库连通的)
                dataSource.setTestOnReturn(testOnReturn);
                //连接空闲时是否进行有效性验证(即是否还和数据库连通的)
                dataSource.setTestWhileIdle(testWhileIdle);
                
                if(StringUtils.isNotBlank(timeBetweenEvictionRunsMillis)){
                     dataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(timeBetweenEvictionRunsMillis));
                }
                
                /** 
                 * 他们两个配合,可以持续更新连接池中的连接对象,当timeBetweenEvictionRunsMillis 大于0时,每过timeBetweenEvictionRunsMillis 时间,
                 * 就会启动一个线程,校验连接池中闲置时间超过minEvictableIdleTimeMillis的连接对象。
                 * */
                if(StringUtils.isNotBlank(timeBetweenEvictionRunsMillis)){
                     dataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(timeBetweenEvictionRunsMillis));
                }
                
                if(StringUtils.isNotBlank(minEvictableIdleTimeMillis)){
                    
                     dataSource.setMinEvictableIdleTimeMillis(Long.parseLong(minEvictableIdleTimeMillis));
                }
              
                try {
                     connection = dataSource.getConnection();
                     System.out.println("从BasicDataSource池中获取连接:"+connection);
                } catch (SQLException e) {
                    e.printStackTrace();
                    log.info("对不起连接数据库失败,请查看您的配置.......");
                }
                
            } catch (IOException e) {
                
                e.printStackTrace();
                
            }finally{
                
                try {
                    if (null!=connection) {
                        //将连接释放到 BasicDataSource池中
                        connection.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    }
}

posted @ 2015-11-06 11:04  yu0312chao  阅读(14810)  评论(0编辑  收藏  举报