C3P0数据源使用Demo

package com.laoxu.test.day04.c3p0DBSource;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * C3P0数据源测试类
 * 
 */
public class C3P0DBSTest {
    /*
     * 因为使用maven管理,因此路径在使用File时不带src/main/... 显示false
     */
    private static final String DB_CONFIG_URL = "com/laoxu/test/day04/c3p0DBSource/DBConfig.properties";
    private static String driver = "";
    private static String url = "";
    private static String username = "";
    private static String password = "";
    private static String acquireRetryAttempts = "";// 定义从数据库创建连接失败重复尝试的时间。default:30
    private static String acquireRetryDelay = "";// 两次连接间隔时间.default:1000
    private static String autoCommitOnClose = "";// 连接关闭时默认将所有未提交的操作回滚。Default:false
    private static String automaticTestTable = "";// c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试使用。Default:null
    private static String breakAfterAcquireFailure = "";// 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
    // 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
    // 获取连接失败后该数据源将申明已断开并永久关闭。Default: false
    private static String idleConnectionTestPeriod = "";// 每60秒检查所有连接池中的空闲连接。Default:
                                                        // 0
    private static String initialPoolSize = "";// 初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default:
                                                // 3
    private static String maxIdleTime = "";// 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default:
                                            // 0
    private static String maxPoolSize = "";// 接池中保留的最大连接数。Default: 15
    private static String numHelperThreads = "";// c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能
    // 通过多线程实现多个操作同时被执行。Default: 3
    
    
    private static ComboPooledDataSource cpds;//c3p0数据源
    /*
     * 静态块,加载数据源配置
     */
    static {
        System.out.println("-----------加载静态块--------------");
        Properties prop = new Properties();
        InputStream is = C3P0DBSTest.class.getClassLoader()
                .getResourceAsStream(DB_CONFIG_URL);
        
        try {
            prop.load(is);
            url = prop.getProperty("url");
            driver = prop.getProperty("driver");
            username = prop.getProperty("username");
            password = prop.getProperty("password");
            maxPoolSize = prop.getProperty("maxPoolSize");
            numHelperThreads = prop.getProperty("numHelperThreads");
            initialPoolSize = prop.getProperty("initialPoolSize");
            
            cpds = new ComboPooledDataSource();
            cpds.setJdbcUrl(url);
            try {
                cpds.setDriverClass(driver);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
            cpds.setUser(username);
            cpds.setPassword(password);
            cpds.setInitialPoolSize(Integer.valueOf(initialPoolSize));
            cpds.setMaxPoolSize(Integer.valueOf(maxPoolSize));
            cpds.setNumHelperThreads(Integer.valueOf(numHelperThreads));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取连接
    public static Connection getCon(){
        try {
            return cpds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    

    public static void main(String[] args) {
        Connection con = getCon();
        System.out.println("con : "+con);
    }
}

 

posted @ 2013-10-23 20:18  prison  阅读(643)  评论(0)    收藏  举报