dbcp 连接 测试 Demo

package com.data.dbcp;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

/**
* DBCP使用的连接方法 需要用到的jar包有 commons-pool-1.4.jar commons-dbcp-1.2.2.jar
* commons-collections-3.2.1.jar
*
* 2. dbcp的基本配置
*
* 相关配置说明:
*
*
*
* initialSize :连接池启动时创建的初始化连接数量(默认值为0) maxActive
* :连接池中可同时连接的最大的连接数(默认值为8,调整为20,高峰单机器在20并发左右,自己根据应用场景定)
* maxIdle:连接池中最大的空闲的连接数,超过的空闲连接将被释放
* ,如果设置为负数表示不限制(默认为8个,maxIdle不能设置太小,因为假如在高负载的情况下,连接的打开时间比关闭的时间快,会引起连接池中idle的个数
* 上升超过maxIdle,而造成频繁的连接销毁和创建,类似于jvm参数中的Xmx设置)
* minIdle:连接池中最小的空闲的连接数,低于这个数量会被创建新的连接
* (默认为0,调整为5,该参数越接近maxIdle,性能越好,因为连接的创建和销毁,都是需要消耗资源的
* ;但是不能太大,因为在机器很空闲的时候,也会创建低于minidle个数的连接,类似于jvm参数中的Xmn设置) maxWait
* :最大等待时间,当没有可用连接时
* ,连接池等待连接释放的最大时间,超过该时间限制会抛出异常,如果设置-1表示无限等待(默认为无限,调整为60000ms,避免因线程池不够用
* ,而导致请求被无限制挂起)
* poolPreparedStatements:开启池的prepared(默认是false,未调整,经过测试,开启后的性能没有关闭的好。)
* maxOpenPreparedStatements:开启池的prepared 后的同时最大连接数(默认无限制,同上,未配置)
* minEvictableIdleTimeMillis :连接池中连接,在时间段内一直空闲, 被逐出连接池的时间
* (默认为30分钟,可以适当做调整,需要和后端服务端的策略配置相关) removeAbandonedTimeout
* :超过时间限制,回收没有用(废弃)的连接(默认为 300秒,调整为180) removeAbandoned
* :超过removeAbandonedTimeout时间后,是否进 行没用连接(废弃)的回收(默认为false,调整为true)
*
*
* removeAbandoned参数解释: 如果开启了removeAbandoned,当getNumIdle() < 2) and
* (getNumActive() > getMaxActive() - 3)时被触发. 举例当maxActive=20,
* 活动连接为18,空闲连接为1时可以触发"removeAbandoned".但是活动连接只有在没有被使用的时间超
* 过"removeAbandonedTimeout"时才被回收 logAbandoned: 标记当连接被回收时是否打印程序的stack
* traces日志(默认为false,未调整)
*
*
* 一般会是几种情况出现需要removeAbandoned:  代码未在finally释放connection
* , 不过我们都用sqlmapClientTemplate,底层都有链接释放的过程
* 遇到数据库死锁。以前遇到过后端存储过程做了锁表操作,导致前台集群中连接池全都被block住,后续的业务处理因为拿不到链接所有都处理失败了。
*
*
* @author bowei.hu
*
*/
public class DbcpConnection {
private static DataSource dataSource;
private static Connection con;

public DbcpConnection() {
}

public static Connection getConnection() {
if (dataSource == null) {
initDataSource();
}
try {
con = dataSource.getConnection();
print();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;

}

public static void initDataSource() {
FileInputStream is = null;
Properties p = new Properties();
String driverClassName = null;
String url = null;
String username = null;
String password = null;
int initialSize = 0;
int minIdle = 0;
int maxIdle = 0;
int maxWait = 0;
int maxActive = 0;
try {
String path = DbcpConnection.class.getClass().getResource("/")
.getPath();
is = new FileInputStream(path + "dbcp.properties");
p.load(is);
driverClassName = p.getProperty("dbcp.driverClassName");
url = p.getProperty("dbcp.url");
username = p.getProperty("dbcp.username");
password = p.getProperty("dbcp.password");

initialSize = Integer.parseInt(p.getProperty("dbcp.initialSize"));
minIdle = Integer.parseInt(p.getProperty("dbcp.minIdle"));
maxIdle = Integer.parseInt(p.getProperty("dbcp.maxIdle"));
maxWait = Integer.parseInt(p.getProperty("dbcp.maxWait"));
maxActive = Integer.parseInt(p.getProperty("dbcp.maxActive"));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

BasicDataSource ds = new BasicDataSource();
ds.setUrl(url);
ds.setDriverClassName(driverClassName);
ds.setUsername(username);
ds.setPassword(password);

ds.setInitialSize(initialSize); // 初始的连接数;
ds.setMaxActive(maxActive);
ds.setMinIdle(minIdle);
ds.setMaxIdle(maxIdle);
ds.setMaxWait(maxWait);
// ds.setRemoveAbandoned(true);
// ds.setRemoveAbandonedTimeout(2000);
dataSource = ds;

}

/* 用于测试连接状态的方法 */
public static void print() {
BasicDataSource ds = (BasicDataSource) dataSource;
System.out.println(ds.getInitialSize());
System.out.println(ds.getNumActive());
System.out.println(ds.getNumIdle());
System.out.println(ds.getDefaultAutoCommit());
}

public static void main(String[] args) {

Connection con;
try {
con = DbcpConnection.getConnection();
print();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from org_user");
while (rs.next()) {
System.out.print("Number:"
+ rs.getString("FIRST_NAME").toString());
System.out.println();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}






---------------------------------------------------------
位于src目录下的dbcp.properties 配置文件
dbcp.driverClassName=oracle.jdbc.driver.OracleDriver
dbcp.url=jdbc:oracle:thin:@127.0.0.1:1521:数据库
dbcp.username=帐号
dbcp.password=密码
dbcp.initialSize=10
dbcp.minIdle=5
dbcp.maxIdle=15
dbcp.maxWait=3000
dbcp.maxActive=20


猜你喜欢

转载自hubowei1.iteye.com/blog/1198212