c3p0实现连接池

编译及运行环境:winxp+jdk5.0+eclipse3.2+oracle9i
所需jar包:
    oracle9.jar(oralce驱动器)
    c3p0-0.9.1.2.jar
    log4j-1.2.8.jar
    commons-logging.jar

SystemConfigResources.properties

#oracle config
DRIVER_NAME=oracle.jdbc.driver.OracleDriver
DATABASE_URL=jdbc:oracle:thin:@127.0.0.1:1521:WORK
DATABASE_USER=system
DATABASE_PASSWORD=manager
#c3p0 config
Initial_PoolSize=10
Min_PoolSize=10
Max_PoolSize=50
Acquire_Increment=10
TIMEOUT=5000
MAX_IdleTime=1800
Idle_Test_Period=3000
Validate=true

SystemConfig.java
package com.database.config;
import java.util.*;
public class SystemConfig {
    static String configFile = "com.database.config.SystemConfigResources";
    public static String getConfigInfomation(String itemIndex) {
        try {
            ResourceBundle resource = ResourceBundle.getBundle(configFile);
            return resource.getString(itemIndex);
        } catch (Exception e) {
            return "";
        }
    }
}

DatabaseAccessInterface.java
package com.database;
import java.sql.*;
import java.util.*;
public interface DatabaseAccessInterface {
    public abstract void executeSQL(String sqlStatement) throws SQLException;
    public abstract void executeSQL(String[] sqlStatement) throws SQLException;
    public abstract void executeSQL(String sqlStatement, Object parameters[]) throws SQLException;
    public abstract void executeSQL(String sqlStatement, List parameters) throws SQLException;
    public abstract void executeSQL(String[] sqlStatement, List parameters) throws SQLException;
    public abstract Vector executeQuerySQL(String sqlStatement) throws SQLException;
    public abstract Vector executeQuerySQL(String sqlStatement, Object parameters[]) throws SQLException;
    public abstract Vector executeQuerySQL(String sqlStatement, List parameters) throws SQLException;
    public abstract String getSequenceNum(String tableName) throws SQLException;
}

DatabaseAccessFactory.java
package com.database;
public class DatabaseAccessFactory {
    private static DatabaseAccessInterface databaseai = null;
    public static DatabaseAccessInterface getDataAccessInstance() {
        if (databaseai == null) {
            databaseai = new DatabaseAccessImpl();
        }
        return databaseai;
    }
}

DBConnectionManager.java
package com.database;
import java.sql.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.database.config.*;
import com.mchange.v2.c3p0.*;
public class DBConnectionManager {
    private static Log                   log = LogFactory.getLog(DatabaseAccessImpl.class);
    private static ComboPooledDataSource cpds = null;
    public static void init() {
        log.debug(">>>>>>>>>>>>>>>>>>>>> <DBConnectionManager.init> Begin");
        // 建立数据库连接池
        String DRIVER_NAME = SystemConfig.getConfigInfomation("DRIVER_NAME"); // 驱动器
        String DATABASE_URL = SystemConfig.getConfigInfomation("DATABASE_URL"); // 数据库连接url
        String DATABASE_USER = SystemConfig.getConfigInfomation("DATABASE_USER"); // 数据库用户名
        String DATABASE_PASSWORD = SystemConfig.getConfigInfomation("DATABASE_PASSWORD"); // 数据库密�码
        int Min_PoolSize = 5;
        int Max_PoolSize = 50;
        int Acquire_Increment = 5;
        int Initial_PoolSize = 10;
        int Idle_Test_Period = 3000;// 每隔3000s测试连接是否可以正常使用
        String Validate = SystemConfig.getConfigInfomation("Validate");// 每次连接验证连接是否可用
        if (Validate.equals("")) {            Validate = "false";        }
        // 最小连接数
        try {
            Min_PoolSize = Integer.parseInt(SystemConfig.getConfigInfomation("Min_PoolSize"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // 增量条数
        try {
            Acquire_Increment = Integer.parseInt(SystemConfig.getConfigInfomation("Acquire_Increment"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // 最大连接数
        try {
            Max_PoolSize = Integer.parseInt(SystemConfig.getConfigInfomation("Max_PoolSize"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // 初始化连接数
        try {
            Initial_PoolSize = Integer.parseInt(SystemConfig.getConfigInfomation("Initial_PoolSize"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // 每隔3000s测试连接是否可以正常使用
        try {
            Idle_Test_Period = Integer.parseInt(SystemConfig.getConfigInfomation("Idle_Test_Period"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        try {
            cpds = new ComboPooledDataSource();
            cpds.setDriverClass(DRIVER_NAME); // 驱动器
            cpds.setJdbcUrl(DATABASE_URL); // 数据库url
            cpds.setUser(DATABASE_USER); // 用户名
            cpds.setPassword(DATABASE_PASSWORD); // 密码
            cpds.setInitialPoolSize(Initial_PoolSize); // 初始化连接池大小
            cpds.setMinPoolSize(Min_PoolSize); // 最少连接数
            cpds.setMaxPoolSize(Max_PoolSize); // 最大连接数
            cpds.setAcquireIncrement(Acquire_Increment); // 连接数的增量
            cpds.setIdleConnectionTestPeriod(Idle_Test_Period); // �测连接有效的时间间隔
            cpds.setTestConnectionOnCheckout(Boolean.getBoolean(Validate)); // 每次连接验证连接是否可用
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        log.debug(">>>>>>>>>>>>>>>>>>>>> <DBConnectionManager.init> End");
    }

    public static Connection getConnection() {// 获取数据库连接
        Connection connection = null;
        try {
            if (cpds == null) {
                init();
            }
            connection = cpds.getConnection(); // getconnection
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return connection;
    }

    public static void release() {
        try {
            if (cpds != null) {
                cpds.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值