天天看點

c3p0連接配接池的用法c3p0連接配接池的用法

c3p0連接配接池的用法

1、導入c3p0jar包

c3p0-0.9.1.2.jar

2、xml配置檔案

在src的目錄下, 建立一個名稱為 c3p0-config.xml的配置檔案, 配置資訊如下:

1)最基本配置*四個連接配接資料庫必須的參數)

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://106.13.43.205:3306/test</property>
        <property name="user">root</property>
        <property name="password">aaaaa123</property>
    </default-config> 
</c3p0-config>            

2)複雜的配置資訊

<?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/test</property>  
        <property name="user">root</property>  
        <property name="password">123456</property>  
             <!--當連接配接池中的連接配接耗盡的時候c3p0一次同時擷取的連接配接數。Default:3 -->  
        <property name="acquireIncrement">5</property>  
             <!--初始化的連接配接數,取值應在minPoolSize與maxPoolSize之間。Default: 3-->  
        <property name="initialPoolSize">10</property>  
             <!--連接配接池中保留的最小連接配接數-->  
        <property name="minPoolSize">5</property>  
             <!--連接配接池中保留的最大連接配接數。Default:15 -->  
        <property name="maxPoolSize">20</property>  
             <!--定義在從資料庫擷取新連接配接失敗後重複嘗試的次數。Default: 30 -->  
        <property name="acquireRetryAttempts">30</property>  
             <!--兩次連接配接中間隔時間,機關毫秒。Default: 1000 -->  
        <property name="acquireRetryDelay">1000</property>  
             <!--連接配接關閉時預設将所有未送出的操作復原。Default: false -->  
         <property name="autoCommitOnClose">false</property>  
    </default-config> 
</c3p0-config>           

3、建立資料源DataSourceUtils(工具類)

package com.canghe.utils;

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 DataSourceUtils {

    private static DataSource dataSource = new ComboPooledDataSource();

    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

    // 直接可以擷取一個連接配接池
    public static DataSource getDataSource() {
        return dataSource;
    }

    // 擷取連接配接對象
    public static Connection getConnection() throws SQLException {

        Connection con = tl.get();
        if (con == null) {
            con = dataSource.getConnection();
            tl.set(con);
        }
        return con;
    }

    // 開啟事務
    public static void startTransaction() throws SQLException {
        Connection con = getConnection();
        if (con != null) {
            con.setAutoCommit(false);
        }
    }

    // 事務復原
    public static void rollback() throws SQLException {
        Connection con = getConnection();
        if (con != null) {
            con.rollback();
        }
    }

    // 送出并且 關閉資源及從ThreadLocall中釋放
    public static void commitAndRelease() throws SQLException {
        Connection con = getConnection();
        if (con != null) {
            con.commit(); // 事務送出
            con.close();// 關閉資源
            tl.remove();// 從線程綁定中移除
        }
    }

    // 關閉資源方法
    public static void closeConnection() throws SQLException {
        Connection con = getConnection();
        if (con != null) {
            con.close();
        }
    }

    public static void closeStatement(Statement st) throws SQLException {
        if (st != null) {
            st.close();
        }
    }

    public static void closeResultSet(ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
    }

}
           

4、使用DButils進行連接配接并對資料庫進行操作

QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from product";
        List<Product> productList = null;
        try {
            productList = runner.query(sql, new BeanListHandler<Product>(Product.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }