import java.sql.connection;
import oracle.jdbc.pool.oracleconnectioncacheimpl;
/**
* 连接池管理类
*/
public class dbpool {
/** 连接池 */
private static oracleconnectioncacheimpl pool = null;
/**
* 初始化连接池
*/
public static void init() {
try{
string user = "user"; //用户名
string password = "pwd"; //密码
string host = "127.0.0.1"; //server地址
string port = "1521"; //端口
string sid = "oracle92"; //sid
int max = 20; //最大连接数
int min = 5; //最小连接数
//拼连接字符串
string url = "jdbc:oracle:thin:@" + host + ":" + port + ":" + sid;
//实例化连接池并设置参数
pool = new oracleconnectioncacheimpl();
pool.seturl(url);
pool.setpassword(password);
pool.setuser(user);
pool.setmaxlimit(max);
pool.setminlimit(min);
/**
* 设置连接数超限时增加方式.
* dynamic_scheme:是动态增加.用完如果超过上限则关掉.
* 还有两个.1.直接返回null .2.始终等待
*/
pool.setcachescheme(oracleconnectioncacheimpl.dynamic_scheme);
/**
* 如果你已经有个连接池了.
* 比如是oracleconnectionpooldatasource ocpds;
* 可以直接设置.前面的设置就不用了.或者直接写在构造器里.
*/
//pool.setconnectionpooldatasource(ocpds);
}catch(exception ex) {
}
}
/**
* 终了处理.
* 比如在app关掉前调用.
*/
public static void destory() {
if(pool != null) {
try {
pool.close();
pool = null;
}catch(exception ex) {
}
}
}
/**
* 取连接.
* 和其他的连接一样正常用.
* @return 连接
*/
public static connection getconnection() {
try{
connection conn = pool.getconnection();
return conn;
}catch(exception e){
return null;
}
}
/**
* 调试
*/
public static void turnning() {
system.out.println("active conns:"+pool.getactivesize());
system.out.println("cached conns:"+pool.getcachesize());
}
}