1. 程式人生 > >[C3P0]XML檔案配置及使用

[C3P0]XML檔案配置及使用

總檔案c3p0-config.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <default-config>
        <property name="automaticTestTable">con_test</property>
        <property name="checkoutTimeout">30000</property>
        <property name="idleConnectionTestPeriod"
>
30</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements"
>
200</property> <!-- 配置mysql資料庫配置 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">1234</property> <property name="jdbcUrl"
>
jdbc:mysql://localhost:3306/mybatis</property> <user-overrides user="test-user"> <property name="maxPoolSize">10</property> <property name="minPoolSize">1</property> <property name="maxStatements">0</property> </user-overrides> </default-config> <!-- This app is massive! --> <named-config name="intergalactoApp"> <property name="acquireIncrement">50</property> <property name="initialPoolSize">100</property> <property name="minPoolSize">50</property> <property name="maxPoolSize">1000</property> <!-- intergalactoApp adopts a different approach to configuring statement caching --> <property name="maxStatements">0</property> <property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him --> <user-overrides user="master-of-the-universe"> <property name="acquireIncrement">1</property> <property name="initialPoolSize">1</property> <property name="minPoolSize">1</property> <property name="maxPoolSize">5</property> <property name="maxStatementsPerConnection">50</property> </user-overrides> </named-config> </c3p0-config>

工具類Conn:

public class Conn {
    private static ComboPooledDataSource c3p0 = new ComboPooledDataSource ();
    //獲取連結物件
    public static Connection getConnection() throws SQLException{
        Connection connection = c3p0.getConnection();
        return connection;
    }

    //釋放資源
    public static void close (Connection connection){
        if(connection!=null){
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}