DBCP(DataBase connection pool),数据库连接池。是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。单独使用dbcp需要2个包:commons-dbcp.jar,commons-pool.jar由于建立数据库连接是一个非常耗时耗资源的行为,所以通过连接池预先同数据库建立一些连接,放在内存中,应用程序需要建立数据库连接时直接到连接池中申请一个就行,用完后再放回去。
一、配置文件
- 配置文件名称:*.properties
- 配置文件位置:任意,建议src下
- 配置文件内容:properties不能编写中文,不支持在STS中修改,必须使用记事本修改内容,否则中文注释就乱码了
Properties文件示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/java_we?useUnicode=true&characterEncoding=utf8 dbuser=root dbpassword=root #初始化连接 initialSize=10 #最大连接数量 maxActive=50 #最大空闲连接 maxIdle=20 #最小空闲连接 minIdle=5 #超时等待时间以毫秒为单位 6000毫米/1000等于60秒 maxWait=60000 #JDBC驱动建立连接时附带的连接属性的格式必须为这样:[属性名=property;] #注意:"user"与"password"两个属性会被明确地传递,因此这里不需要包含他们。 connectionProperties=useUnicode=true;characterEncoding=gbk #指定由连接池所创建的连接的自动提交(auto-commit)状态 defaultAutoCommit=true #driver default 指定由连接池所创建的连接的只读(read-only)状态。 #如果没有设置该值,则"setReadOnly"方法将不被调用。(某些驱动并不支持只读模式。如:Informix) defaultReadOnly= #driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。 #可用值为下列之一:(详情可见javadoc)NONE,READ_UNCOMMITTED,READ_COMMITTED,REPEATABLE_READ,SERIALIZABLE defaultTransactionIsolation=READ_UNICOMMITTED |
常见配置项
分类 | 属性 | 描述 |
必须项 |
user | 用户名 |
password | 密码 | |
driverClass | 驱动
mysql驱动,com.mysql.jdbc.Driver |
|
jdbcUrl | 路径
mysql路径,jdbc:mysql://localhost:3306/数据库 |
|
基本项 |
maxActive | 最大连接数量 |
minIdle | 最小空闲连接 | |
maxIdle | 最大空闲连接 | |
initialSize | 初始化连接 | |
优化配置(扩展) |
logAbandoned | 连接被泄露时是否打印 |
removeAbandoned | 是否自动回收超时连接 | |
removeAbandonedTimeout | 超时时间(以秒数为单位) | |
maxWait | 超时等待时间以毫秒为单位 1000等于60秒 | |
timeBetweenEvictionRunsMillis | 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位 | |
numTestsPerEvictionRun | 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 | |
minEvictableIdleTimeMillis | 连接在池中保持空闲而不被空闲连接回收器线程 |
参数详见:http://commons.apache.org/proper/commons-dbcp/configuration
二、编写工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package cn.benz.Utils; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; public class DBCPUtils { private static DataSource dataSource; static{ try { //1.加载找properties文件输入流 InputStream is = DBCPUtils.class.getClassLoader().getResourceAsStream("db.properties"); //2.加载输入流 Properties props = new Properties(); props.load(is); //3.创建数据源 dataSource = BasicDataSourceFactory.createDataSource(props); } catch (Exception e) { throw new RuntimeException(e); } } public static DataSource getDataSource(){ return dataSource; } public static Connection getConnection(){ try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } } } |
测试工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package cn.benz.test; import java.sql.Connection; import java.sql.PreparedStatement; import org.junit.Test; import cn.benz.Utils.DBCPUtils; public class TestDBCPUtils { @Test public void testUpdate(){ Connection conn = null; PreparedStatement pstmt = null; try { conn = DBCPUtils.getConnection(); String sql ="update user set username=? where id=?"; pstmt= conn.prepareStatement(sql); pstmt.setString(1, "柳岩"); pstmt.setInt(2, 1); int rows = pstmt.executeUpdate(); if(rows>0){ System.out.println("更新成功!"); }else{ System.out.println("更新失败!"); } } catch (Exception e) { throw new RuntimeException(e); } } } |