数据库连接池
参考:https://baike.baidu.com/item/%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0/1518538?fr=aladdin
原理:连接池基本的思想是在系统初始化的时候,将数据库连接作为对象存储在内存中,当用户需要访问数据库时,并非建立一个新的连接,而是从连接池中取出一个已建立的空闲连接对象。使用完毕后,用户也并非将连接关闭,而是将连接放回连接池中,以供下一个请求访问使用。而连接的建立、断开都由连接池自身来管理。
原因:数据库的连接和释放都非常消耗系统资源,因此池化技术,事先准备好一些数据库连接在内存中,当需要时直接就可以连接,释放时就放回到池子中。这样可以减少对系统资源的消耗
重要参数:1. 最小连接数:是连接池一直保持的数据库连接 2. 最大连接数:是连接池能申请的最大连接数,如果数据库连接请求超过此数,后面的数据库连接请求将被加入到等待队列中,这会影响之后的数据库操作 3、等待超时时间:如果加入等待队列的请求超过了这个时间,就不再等待了,报告异常
注意:在利用连接池技术时,一般都会从一些文件中读取配置,比如properties、xml文件,这些文件中的变量名不能随意改变。
DBCP
commons-dbcp-1.4.jar、commons-pool-1.6-bin.jar
dbcpconfig.properties
配置文件,里面的变量名不能随便改,是固定的
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=123456
#!-- 初始化连接 --
initialSize=10
#最大连接数量
maxActive=50
#!-- 最大空闲连接 --
maxIdle=20
#!-- 最小空闲连接 --
minIdle=5
#!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 --
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:【属性名=property;】
#注意:user 与 password 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=UTF8
#指定由连接池所创建的连接的自动提交(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_UNCOMMITTED
JdbcUtils_DBCP.class
测试类。利用数据源工厂创建数据源。由于配置文件变量名都是固定的,因此不用挨个去读,直接传入就好BasicDataSourceFactory.createDataSource(properties)
。此外也不需要驱动程序了,也可以利用数据源直接获取连接,不用传入参数source.getConnection()
。
public class JdbcUtils_DBCP {
public static DataSource source = null;
static{ //提取配置文件内的信息
try{
//通过反射获取配置文件,并转为流的形式
InputStream stream = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
//新建配置对象,并加载数据
Properties properties = new Properties();
properties.load(stream);
//利用数据源工厂创建数据源,数据源的参数就是我们的配置文件,他会自动提取信息
source = BasicDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
//连接
public static Connection getConnection() throws SQLException {
return source.getConnection();
}
//释放连接
public static void release(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
if(resultSet!=null){
resultSet.close();
}
if(statement!=null){
statement.close();
}
if(connection!=null){
connection.close();
}
}
}
DBCPTest.class
public class DBCPTest {
public static void main(String[] args) throws SQLException {
//初始化变量
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//利用了连接池
connection = JdbcUtils_DBCP.getConnection();
String sql = "select * from account where name = 'A'";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println("id:" + resultSet.getInt("id"));
System.out.println("name:" + resultSet.getString("name"));
System.out.println("money:" + resultSet.getInt("money"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JdbcUtils.release(resultSet, preparedStatement, connection);
}
}
}
C3P0
c3p0-0.9.5.5.jar、mchange-commons-java-0.2.19.jar
设置参数的文件是xml文件
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!--
c3p0的缺省(默认)配置
如果在代码中ComboPooledDataSource ds=new ComboPooledDataSource();这样写就表示使用的是c3p0的缺省(默认)-->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/school?userUnicode=true&characterEncoding=utf8&uesSSL=true</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="acquiredIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
</c3p0-config>
JdbcUtils_C3P0
xml文件不需要load,创建对象时会自动匹配。
new ComboPooledDataSource()如果没有参数,会默认选择xml中default模块。也可以xml中模块赋名字,然后通过给方法传入名字来进行匹配。
public class JdbcUtils_C3P0 {
public static ComboPooledDataSource source = null;
static{
try{
//使用配置文件,不用导入,xml文件会自动匹配
source = new ComboPooledDataSource();
} catch (Exception e) {
e.printStackTrace();
}
}
//连接
public static Connection getConnection() throws SQLException {
return source.getConnection();
}
//释放连接
public static void release(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}
C3P0Test.class
与DBCPTest.class完全一致