MySQL---數據庫從入門走向大神系列(十四)-ComboPooledDataSource(C3P0鏈接池配置)

MySQL的jar包mysql-connector-java-5.1.39-bin目前是5.1.39版本: java

須要準備的jar包:mysql

MySQL的jar包mysql-connector-java-5.1.39-bin目前是5.1.39版本: 
http://dev.mysql.com/downloads/connector/j/sql

C3p0的2個包: 
https://sourceforge.net/projects/c3p0/ 
c3p0-0.9.1.2.jar和c3p0-sources-0.9.1.2.jar數據庫

 

配置文件:c3p0-config.xml 框架

名字必須爲這個!ui

<c3p0-config>
    <!-- 默認配置,若是沒有指定則使用這個配置 -->
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">
            <![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=UTF-8]]>
        </property>
        <property name="user">root</property>
        <property name="password">1234</property>
        <!-- 初始化池大小 -->
        <property name="initialPoolSize">2</property>
        <!-- 最大空閒時間 -->
        <property name="maxIdleTime">30</property>
        <!-- 最多有多少個鏈接 -->
        <property name="maxPoolSize">10</property>
        <!-- 最少幾個鏈接 -->
        <property name="minPoolSize">2</property>
        <!-- 每次最多能夠執行多少個批處理語句 -->
        <property name="maxStatements">50</property>
    </default-config> 
    <!-- 命名的配置 -->
    <named-config name="demo">
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl"><![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=UTF-8]]></property>
        <property name="user">root</property>
        <property name="password">1234</property>
        <property name="acquireIncrement">5</property><!-- 若是池中數據鏈接不夠時一次增加多少個 -->
        <property name="initialPoolSize">100</property>
        <property name="minPoolSize">50</property>
        <property name="maxPoolSize">1000</property>
        <property name="maxStatements">0</property>
        <property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him -->
    </named-config>
</c3p0-config>

 

演示代碼:

package cn.hncu.C3p0;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0Demo {

    @Test
    // 純Java方法使用c3p0
    public void C3p0Demo() throws PropertyVetoException, SQLException {
        ComboPooledDataSource pool = new ComboPooledDataSource();
        pool.setUser("root");// 用戶姓名
        pool.setPassword("1234");// 用戶密碼
        pool.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=utf-8");// MySQL數據庫鏈接url
        pool.setDriverClass("com.mysql.jdbc.Driver");

        // pool能夠調用set方法進行Connection池的配置

        // 鏈接關閉以後,內存會被釋放,下次取時會從新開(內存地址不共用)
        for (int i = 0; i < 20; i++) {
            Connection con = pool.getConnection();
            System.out.println(i + ":" + con);
            if (i % 2 == 0) {
                con.close();
            }
        }
    }

    @Test
    // 演示採用配置文件的方式使用c3p0
    public void C3p0PropertyDemo() throws SQLException {
        ComboPooledDataSource pool = new ComboPooledDataSource();//空參,自動到classpath目錄下面加載「c3p0-config.xml」配置文件---配置文件的存儲位置和名稱必須是這樣,且使用「默認配置」
        //ComboPooledDataSource pool = new ComboPooledDataSource("demo");//加載「c3p0-config.xml」文件中定義的「demo」這個配置元素
         for(int i=0;i<25;i++){
               Connection con = pool.getConnection();
               System.out.println(i+":"+ con.hashCode());
           }
    }

}

 

包裝一下C3P0的Connection池

package cn.hncu.C3p0;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0Pool {
    //咱們的這個包裝,只是爲了把c3p0池作成讓每一個線程(客戶端)得到的是同一個鏈接,方便作b/s框架下的事務
    private static DataSource pool;
    private static ThreadLocal<Connection> t = new ThreadLocal<Connection>();
    static{
        pool = new ComboPooledDataSource("demo");
        //這裏的參數視你的配置文件而定,也能夠不寫,用默認的配置
    }

    public static DataSource getDataSource(){
        return pool;
    }

    public static Connection getConnection() throws SQLException{
        Connection con = t.get();
        if(con==null){
            con=pool.getConnection();
            t.set(con);
        }
        return con;
    }
}
相關文章
相關標籤/搜索