C3P0快速入门

来源:互联网 发布:java并发编程 编辑:程序博客网 时间:2024/06/05 21:07

概述

本文主要目的是帮助使用者快速入门使用C3P0,所以不会在原理及相关概念上做过多文章,并只介绍一些常用的属性。じゃ、始めましょう。

什么是C3P0

它是一个开源的数据库连接池。

为什么要使用C3P0

使用数据库连接池的好处在此不再赘述,那为什么是C3P0而不是别的连接池(DBCP 数据库连接池 )呢?
相比DBCP 数据库连接池 ,C3P0连接池不仅可以自动清理不再使用的Connections,还可以自动清理Statement和Resultset。所以我们是不是就可以不用手动去释放资源了呢?答案是否定的,良好的习惯是程序开发人员必须要培养的,以免在请用其他数据库时,忘记释放资源以导致系统崩溃。

如何使用C3P0

  1. 导入依赖的 jar 包
    c3p0-0.9.2-pre1.jar
    mchange-commons-0.2.jar
    c3p0-oracle-thin-extras-0.9.2-pre1.jar
    因为是开源组织开发的工具,Java官方包中并未包含,所以需要我们手动为项目导入。
    对于使用 MySQL 数据库,导入前两个包就可以。

  2. 创建 c3p0-config.xml 文件
    此处的文件名必须为c3p0-config.xml,配置 xml 文件是最常用的方式,便于以后修改与维护。
    也可以在通过程序中的代码实现(此时,可以跳过步骤2),但相比配置文件的方式而言,并不推荐这种方式,因此本文中不会提供相应的实现,有兴趣的话可以参考官方文档。

  3. 创建 ComboPooledDataSource 实例
    DataSource dataSource = new ComboPooledDataSource(“helloc3p0”);

  4. 从 DataSource 实例中获取数据库连接.
    Connection conn = dataSource.getConntection();

代码示例

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/chatroom</property>        <property name="user">root</property>        <property name="password">123456</property>        <property name="acquireIncrement">50</property>        <property name="initialPoolSize">10</property>        <property name="minPoolSize">5</property>        <property name="maxPoolSize">20</property>    </default-config>    <!--        需要注意的是,named-config 中 name 属性的值是和        DataSource dataSource = new ComboPooledDataSource("helloc3p0");        中构造函数的参数值向对应的。        若是使用默认无参构造函数时,创建 DataSource 对象时,会使用上面的 <default-config> 配置参数    -->    <named-config name="helloc3p0">        <!-- 指定连接数据源的基本属性 -->        <property name="user">root</property>        <property name="password">123456</property>        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="jdbcUrl">jdbc:mysql:///chatroom</property>        <!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->        <property name="acquireIncrement">5</property>        <!-- 初始化数据库连接池时连接的数量 -->        <property name="initialPoolSize">5</property>        <!-- 数据库连接池中的最小的数据库连接数 -->        <property name="minPoolSize">5</property>        <!-- 数据库连接池中的最大的数据库连接数 -->        <property name="maxPoolSize">10</property>        <!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->        <property name="maxStatements">20</property>        <!-- 每个连接同时可以使用的 Statement 对象的个数 -->        <property name="maxStatementsPerConnection">5</property>    </named-config></c3p0-config>

在代码中使用 C3P0

package C3P0Test;import com.mchange.v2.c3p0.ComboPooledDataSource;import javax.sql.DataSource;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class JDBCUtils {    private static ComboPooledDataSource dataSource = new ComboPooledDataSource("helloc3p0");    public  static Connection getConnection() throws SQLException {        Connection connection = dataSource.getConnection();        return connection;    }    public static void release(Connection connection, Statement statement, ResultSet resultSet) {        if (resultSet != null) {            try {                resultSet.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        if (statement != null) {            try {                statement.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        if (connection != null) {            try {                connection.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }}

结束语

至此,你已经得到数据库连接了,请继续你的表演O(∩_∩)O哈哈~

0 0
原创粉丝点击