数据库连接池(C3P0)

发布时间 2023-10-07 14:43:22作者: 北星121

为什么要引入数据库连接池?

在JDBC编程中,每次创建和断开Connection对象都会消耗一定的时间和IO资源。 这是因为在Java程序与数据库之间建立连接时,数据库端要验证用户名和密码并为该连接分配资源,而程序则要把代表连接Connection对象等加载到内存中,所以建立数据库连接的开销很大。尤其是在大量的并发访问时,频繁地创建、断开数据库 连接势必会影响数据库的访问效率,甚至导致数据库崩溃。

为了解决该类问题的发生诞生了数据库连接池技术。数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用现有的数据库连接,而不是每次都重新建立连接。

本文主要介绍C3P0数据库连接池。

C3P0

C3P0是目前最流行的开源数据库连接池之一,它支持 JDBC2和JDBC3的标准规范,易于扩展并且性能优越,著名的开源框架Hibernate和 Spring使用的数据源正是C3P0。

第一步:添加jar包

  • c3p0-0.9.1.2.jar

  • mysql-connector-java-5.0.8-bin.jar

第二步:在src下创建C3P0的配置文件c3p0-config.xml

  • <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
     <default-config>
       <property name="driverClass">com.mysql.jdbc.Driver</property>
       <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb</property>
       <property name="user">root</property>
       <property name="password">root</property>
       <property name="initialPoolSize">15</property>
       <property name="maxIdleTime">40</property>
       <property name="maxPoolSize">150</property>
       <property name="minPoolSize">20</property>
     </default-config>
    </c3p0-config>

    第三步:编写操作C3P0的工具类C3P0Util

  • package cn.com.demo8;

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.sql.DataSource;
    import com.mchange.v2.c3p0.ComboPooledDataSource;

    public class C3P0Util {

    //创建数据库连接池
    private static DataSource dataSource = new ComboPooledDataSource();

    //创建连接
    public static Connection getConnection(){
    try {
    return dataSource.getConnection();
    } catch (SQLException e) {
    throw new RuntimeException("获取数据库连接失败");
    }
    }

    //释放连接
    public static void release(Connection connection, Statement statement, ResultSet resultSet) {
    if (resultSet != null) {
    try {
    resultSet.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    resultSet = null;
    }
    if (statement != null) {
    try {
    statement.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    statement = null;
    }
    if (connection != null) {
    try {
    connection.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    connection = null;
    }
    }
    }

第四步:使用C3P0

public class TestC3P0 {
public static void main(String[] args) {
TestC3P0 demo = new TestC3P0();
demo.testC3P0();
}

public void testC3P0() {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = C3P0Util.getConnection();
preparedStatement = connection.prepareStatement("SELECT * FROM student");
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Student student = new Student();
int id = resultSet.getInt("studentid");
String name = resultSet.getString("studentname");
student.setStudentID(id);
student.setStudentName(name);
System.out.println(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
C3P0Util.release(connection, preparedStatement, resultSet);
}

}