c3p0數句庫連接池的使用

注:c3p0的使用需要導jar包—c3p0-0.9.1.2.jar

public class C3p0Demo {

    @Test
    public void c3p0Demo() throws PropertyVetoException, SQLException{
        ComboPooledDataSource pool = new ComboPooledDataSource();
        pool.setUser("root");
        pool.setPassword("1234");
        pool.setDriverClass("com.mysql.jdbc.Driver");
        pool.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/aa?characterEncoding=utf8&useSSL=true");

        pool.setMaxPoolSize(1000);
        for(int i=0;i<1000;i++){
            Connection con = pool.getConnection();
            System.out.println(i+":"+con);
        }
    }

    @Test//這種方式是通過配置文件方式載入,配置文件放在src根目錄下(classpath目錄)
    public void c3p0PropertyDemo() throws SQLException{
        ComboPooledDataSource pool = new ComboPooledDataSource("hncu");
        pool.setMaxPoolSize(1000);
        for(int i=0;i<1000;i++){
            Connection con = pool.getConnection();
            System.out.println(i+":"+con);
        }
    }
}

c3p0-config.xml文件內容

<c3p0-config>
    <!-- 默認配置,如果沒有指定則使用這個配置 -->
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">
            <![CDATA[jdbc:mysql://127.0.0.1:3306/mydata?useUnicode=true&characterEncoding=UTF-8&useSSL=true]]>
        </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="hncu">
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/aa</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> 

自定義c3p0池:

public class C3p0Pool {
    private static DataSource pool = null;
    private static ThreadLocal<Connection> t = new ThreadLocal<Connection>();

    static{
        pool = new ComboPooledDataSource();
    }

    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;
    }
}
發佈了76 篇原創文章 · 獲贊 53 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章