jdbc.properties配置文件:
jdbc.driverClassName=com.ibm.db2.jcc.DB2Driver
#---------------------------------------------------
DEVELOP DATABASE
jdbc.url=jdbc:db2://10.10.0.163:50000/MACRODB
#jdbc.url=jdbc:db2://10.10.0.154:50000/SAMPLE
#---------------------------------------------------
#TEST DATABASE
#jdbc.url=jdbc:oracle:thin:@192.168.1.100:1521:orcl
#---------------------------------------------------
#LOCALHOST DATABASE
#jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
#jdbc.username=db2inst1
#jdbc.password=db2inst1
jdbc.username=db2inst1
jdbc.password=123456
c3p0.acquireIncrement=3
c3p0.initialPoolSize=3
c3p0.minPoolSize=10
c3p0.maxPoolSize=15
c3p0.maxIdleTime=30
c3p0.idleConnectionTestPeriod=30
c3p0.maxStatements=100
c3p0.numHelperThreads=50
c3p0.checkoutTimeout=0
c3p0.validate=true
读取配置文件:
package com.nci.macrodb.core.sql;
import java.util.ResourceBundle;
/**
*取得资源文件
*
*@authorldw
*
*/
publicclass C3P0SystemConfig {
static String configFile = "spring/jdbc";//根据具体配置文件名称配置
/**
*根据属性名得到资源属性
*
*@paramitemIndex
*@return
*/
publicstatic String getConfigInfomation(String itemIndex) {
try {
ResourceBundle resource = ResourceBundle.getBundle(configFile);
return resource.getString(itemIndex);
} catch (Exception e) {
return"";
}
}
}
获得连接:
package com.nci.macrodb.core.sql;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 编程调用c3p0
*
* @author xuhua
*
*/
public class C3P0DBConnectionManager {
private static ComboPooledDataSource cpds = null;
/**
* 初始化
*/
public static void init() {
// 建立数据库连接池
String DRIVER_NAME = C3P0SystemConfig
.getConfigInfomation("jdbc.driverClassName"); // 驱动器
String DATABASE_URL = C3P0SystemConfig.getConfigInfomation("jdbc.url"); // 数据库连接url
String DATABASE_USER = C3P0SystemConfig
.getConfigInfomation("jdbc.username"); // 数据库用户名
String DATABASE_PASSWORD = C3P0SystemConfig
.getConfigInfomation("jdbc.password"); // 数据库密码
int Min_PoolSize = 5;
int Max_PoolSize = 50;
int Acquire_Increment = 5;
int Initial_PoolSize = 10;
// 每隔3000s测试连接是否可以正常使用
int Idle_Test_Period = 3000;
// 每次连接验证连接是否可用
String Validate = C3P0SystemConfig.getConfigInfomation("c3p0.validate");
if (Validate.equals("")) {
Validate = "false";
}
// 最小连接数
try {
Min_PoolSize = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.minPoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 增量条数
try {
Acquire_Increment = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.acquireIncrement"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 最大连接数
try {
Max_PoolSize = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.maxPoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 初始化连接数
try {
Initial_PoolSize = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.initialPoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 每隔Idle_Test_Period s测试连接是否可以正常使用
try {
Idle_Test_Period = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.idleConnectionTestPeriod"));
} 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();
}
}
/**
* 取得链接
*
* @return
*/
public static Connection getConnection() {
Connection connection = null;
try {// 保证只进行一次初始化
if (cpds == null) {
init();
}
// 取得connection
connection = cpds.getConnection();
} catch (SQLException ex) {
ex.printStackTrace();
}
return connection;
}
/**
* 释放连接
*/
public static void release() {
try {
if (cpds != null) {
cpds.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
posted on 2009-12-11 22:33
孤飞燕 阅读(1775)
评论(0) 编辑 收藏 所属分类:
Java