1.为什么需要连接池:
在J2EE开发中,性能损耗之一就是不断的去开辟IO流,在获取Conection对象连接数据库后,用户查询出Result程序就关闭连接了,这样一开一关是相当消耗性能的
2.连接池所需要的属性:
1.JDBC开发中的基本属性:
private String driverName = "com.mysql.jdbc.Driver";//JDBC驱动名称
private String url = "jdbc:mysql://127.0.0.1/databaseName?characterEncoding=utf8";//JDBC地址
private String userName = "root";//数据库用户名
private String password = "root";//数据库密码
2.连接池属性:
private String poolName = driverName+"_"+url;//连接池名称(一般由DriverName+"_"+url)
private Integer minConnections = 5;//连接池空闲时最小连接数
private Integer maxConnections = 20;//连接池空闲时最大连接数
private Integer initConnections = 10;//连接池初始化连接数
private Integer connTimeOut = 1000L;//无法获取连接下等待时间
private Integer maxActiveConnections = 100;//最大允许连接数,mysql默认100
private Boolean isCurrentConnection = true;//是否获取当前连接
private Boolean isCheckPool = true;//是否定时检查连接池
private Long lazyCheck = 1000*60*60L;//延迟检查时间
private Long periodCheck = 1000*60*60L;//检查频率
3.code:
package com.youxuan.databases;
public class DBbean {
private String driverName = "com.mysql.jdbc.Driver"; //JDBC驱动
private String url = "jdbc:mysql://127.0.0.1:3306/youxuan?characterEncoding=utf-8"; //JDBC地址
private String userName = "root"; //数据库用户名
private String password = ""; //数据库密码
private String poolName = "testPool"; //连接池名字
private Integer minConnections = 3; //空闲时最小连接数
private Integer maxConnections = 10; //空闲时最大连接数
private Integer initConnection = 5; //初始化连接数量
private Long connTimeOut = 1000L; //重复获得连接的频率
private Integer maxActiveConnections = 100; //最大允许的连接数
private Long ConnectionTimeOut = 1000*60*20L; //连接超时时间
private Boolean isCurrentConnection = true;//是否获得当前连接
private Boolean isCh