版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40178464/article/details/82024117
DBCP(DataBase connection pool),数据库连接池。是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。单独使用dbcp需要3个包:commons-dbcp.jar,commons-pool.jar和commons-connections.jar,由于建立数据库连接是一个非常耗时耗资源的行为,所以通过连接池预先同数据库建立一些连接,放在内存中,应用程序需要建立数据库连接时直接到连接池中申请一个就行,用完后再放回去。
一、使用步骤:
1.编写配置文件dbcpconfig.properties
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=******
#初始化连接
initialSize=5
#最大连接数量
maxActive=10
#最大空闲连接
maxIdle=6
#最小空闲连接
minIdle=2
#超时等待时间,以毫秒为单位
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
connectionProperties=useUnicode=true;characterEncoding=gbk
#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的只读(read-only)状态。
defaultReadOnly=
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)
defaultTransactionIsolation=READ_UNCOMMITTED
2.优化jdbcUtils类:
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* 工具类
*/
public final class JdbcUtils {
/**
* 连接池类对象
*/
private static DataSource dataSource;
private JdbcUtils() {
}
/**
* 注册驱动
*/
static {
try {
Class.forName("com.mysql.jdbc.Driver");
// 利用Properties集合加载数据源
Properties prop = new Properties();
InputStream is = JdbcUtils.class.getClassLoader()
.getResourceAsStream("dbcpconfig.properties");
prop.load(is);
// 利用工厂模式创建数据库
dataSource = BasicDataSourceFactory.createDataSource(prop);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
/**
* 建立连接
*
* @return
* @throws SQLException
*/
public static Connection getConnect() throws SQLException {
return dataSource.getConnection();
}
/**
* 释放资源
*
* @param rs
* @param st
* @param conn
*/
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
3.测试:
public static void main(String[] args) throws SQLException {
for (int i = 0; i < 10; i++) {
Connection connection = JdbcUtils.getConnect();
System.out.println(connection);
System.out.println(connection.getClass().getName());
JdbcUtils.free(null, null, connection);
}
}
结果:
733957003, URL=jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
55331187, URL=jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
1991294891, URL=jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
198761306, URL=jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
1263877414, URL=jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
1208736537, URL=jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
712256162, URL=jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
1182461167, URL=jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
561247961, URL=jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
1863932867, URL=jdbc:mysql://localhost:3306/test, UserName=root@localhost, MySQL Connector Java
org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
二、DBCP在IDEA的配置过程
1.首先从Apache官网下载所需要的三个依赖jar包,地址:http://commons.apache.org/proper/
2.打开idea的项目属性,导入需要的jar包:
三、遇到的问题
按照上面的步骤操作下来,会发现出现异常:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
原因是还缺少commons-logging.jar包,从上面的网址中找到对应的jar包,导入到前面几个jar包 的位置就好