1:DBConn.java
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.szmsd.log.util.Const;
public class DBConn
{
private static Log logs = LogFactory.getLog(DBConn.class);
private static ComboPooledDataSource cpds = null;
private static Const c = new Const();
@SuppressWarnings("static-access")
public static void init()
{
logs.info("begin init dbpool");
// 建立数据库连接池
try
{
cpds = new ComboPooledDataSource();
cpds.setDriverClass(c.driverClassName); // 驱动器
cpds.setJdbcUrl(c.url); // 数据库url
cpds.setUser(c.username); // 用户名
cpds.setPassword(c.password); // 密码
cpds.setInitialPoolSize(Integer.parseInt(c.initSize)); // 初始化连接池大小
cpds.setMinPoolSize(Integer.parseInt(c.minSize)); // 最少连接数
cpds.setMaxPoolSize(Integer.parseInt(c.maxSize)); // 最大连接数
cpds.setAcquireIncrement(Integer.parseInt(c.acquireIncrement)); // 连接数的增量
cpds.setIdleConnectionTestPeriod(Integer.parseInt(c.idleConnectionTestPeriod)); // 隔多少秒检查所有连接池中的空闲连接,默认为0表示不检查;
//cpds.setTestConnectionOnCheckout(true); // 每次连接验证连接是否可用
//cpds.setMaxStatements(500);
//cpds.setMaxStatementsPerConnection(5);
} catch (Exception ex) {
logs.error("init dbpool failed",ex);
}
logs.info("dbpool inited");
}
//
// 获取数据库连接
//
public static Connection getConnection() {
Connection connection = null;
try
{
if (cpds == null)
{
init();
}
connection = cpds.getConnection();
}
catch (SQLException ex)
{
logs.error("get dbconn failed",ex);
}
return connection;
}
//
// 释放连接池
//
public static void release() {
try
{
if (cpds != null)
{
cpds.close();
}
}
catch (Exception ex)
{
logs.error("release dbpool failed",ex);
}
}
public static void main(String[] args)
{
System.out.println(DBConn.getConnection());
}
}
2:配置信息
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@183.3.221.238:1589:or
username=yinnibendi
password=yne3new
initSize=30
maxSize=150
minSize=5
acquireIncrement=10
idleConnectionTestPeriod=0