数据库连接池之c3p0

本文介绍如何在Java项目中使用C3P0连接池,包括配置文件的设置及DataSource的实例化方法。提供了详细的配置参数说明,并展示了通过配置文件与Java代码两种方式初始化连接池的过程。


使用c3p0连接池初始化DataSource有两种方式,一种可以在Java代码中编码连接池信息,一种是在配置文件中配置连接池信息。

配置文件形式

例如在Java 的Maven项目中,引入c3p0和数据库驱动:

<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>

配置文件放在resources下,文件名称为c3p0-config.xml:

<c3p0-config>

    <default-config>
        <property name="maxPoolSize">30</property>
        <property name="minPoolSize">10</property>
    </default-config>

    <named-config name="jt-datasource">
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://10.47.98.144:3306/quickview?characterEncoding=utf8&useSSL=false</property>

        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize">3</property>
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize">50</property>
        <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
        属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
        如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
        <property name="maxStatements">100</property>
        <!--c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能
        通过多线程实现多个操作同时被执行。Default: 3-->
        <property name="numHelperThreads">3</property>
        <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime">60</property>
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement">3</property>
        <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts">30</property>
        <!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。Default: 0 -->
        <property name="checkoutTimeout">3000</property>
        <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod">60</property>

    </named-config>

</c3p0-config>

全局实例化唯一DataSource

/**
 * 数据库连接池
 * Created by weifeng on 2017/4/1.
 */
public class DataSource {

    private static Logger LOG = Logger.getLogger(DataSource.class);
    private static ComboPooledDataSource cpds = null;

    private static DataSource instance = null;

    private DataSource() {

    }

    static {
        cpds = new ComboPooledDataSource("jt-datasource");
//            cpds = new ComboPooledDataSource();
//            cpds.setJdbcUrl("jdbc:mysql://10.47.98.144:3306/quickview?characterEncoding=utf8&useSSL=false");
//            cpds.setDriverClass("com.mysql.jdbc.Driver");
//            cpds.setUser("root");
//            cpds.setPassword("root");
    }

    public static DataSource getInstance() {
        if (instance == null) {
            synchronized (DataSource.class) {
                if (instance == null) {
                    instance = new DataSource();
                }
            }
        }
        return instance;
    }

    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = cpds.getConnection();
        } catch (SQLException e) {
            LOG.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        return connection;
    }

    public static void close(ResultSet rs, Statement statement, Connection connection) {
        try {
            if (rs != null) {
                rs.close();
            }

            if (statement != null) {
                statement.close();
            }

            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            LOG.error(e.getMessage());
        }
    }

    public static void close(Statement statement, Connection connection) {
        try {
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            LOG.error(e.getMessage());
        }
    }

    public static void close(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            LOG.error(e.getMessage());
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值