您的位置:首页 > 其它

commons DBCP 配置参数简要说明

2007-01-23 16:37 507 查看
今天的工作很轻松,所以下午的时候去翻看了以前的代码(也是公司前辈们的结晶),最近一直在做网站,大大小小已经作了好多个了,有一些公用的方法几乎都是拿过来就用,看都没有看,现在有时间了,就拿来研究下。第一个看的就是数据库连接部分,没想到看这个代码用了我差不多半天的时间~

以前都在用,但是对里面很多的地方都不是很了解,公司里也用了数据池连接,这个还真的不了~ 所以找了点资料,看了一知半解的,所以保存下来以后反复学习~

使用dbcp做为数据库连接池- -

在数据库应用中,数据库连接过程需要较长的时间。
而且,频繁的连接数据库会增加数据库系统的压力。
所以,最好在项目中使用数据库连接池来减少数据库连接的数量提高数据库访问效率。

DBCP是Apache的一个开源项目:
commons.dbcp http://jakarta.apache.org/commons/dbcp/index.html
DBCP依赖Apache的另外2个开源项目
commons.collections

commons.pool

下载这些包并将这些包的路径添加到classpath中就可以使用dbcp做为项目中的数据库连接池使用了。

commons DBCP 配置参数简要说明 (转载)
在配置时,主要难以理解的主要有:removeAbandoned 、logAbandoned、removeAbandonedTimeout、maxWait这四个参数,设置了rmoveAbandoned=true那么在getNumActive()快要到getMaxActive()的时候,系统会进行无效的Connection的回收,回收的Connection为removeAbandonedTimeout(默认300秒)中设置的秒数后没有使用的Connection,激活回收机制好像是getNumActive()=getMaxActive()-2。 :) 有点忘了。
  logAbandoned=true的话,将会在回收事件后,在log中打印出回收Connection的错误信息,包括在哪个地方用了Connection却忘记关闭了,在调试的时候很有用。
  在这里建议maxWait的时间不要设得太长,maxWait如果设置太长那么客户端会等待很久才激发回收事件。
  以下是我的配置的properties文件:


#连接设置


jdbc.driverClassName=oracle.jdbc.driver.OracleDriver


jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:DBSERVER


jdbc.username=user


jdbc.password=pass




#<!-- 初始化连接 -->


dataSource.initialSize=10




#<!-- 最大空闲连接 -->


dataSource.maxIdle=20




#<!-- 最小空闲连接 -->


dataSource.minIdle=5




#最大连接数量


dataSource.maxActive=50




#是否在自动回收超时连接的时候打印连接的超时错误


dataSource.logAbandoned=true




#是否自动回收超时连接


dataSource.removeAbandoned=true




#超时时间(以秒数为单位)


dataSource.removeAbandonedTimeout=180




#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->


dataSource.maxWait=1000



 以下是我在连接控制中调用的方法:


Properties dbProps=null;


  //下面的读取配置文件可以根据实际的不同修改


dbProps = ConfigProperties.getInstance().getProperties("jdbc.properties");




try ...{


String driveClassName = dbProps.getProperty("jdbc.driverClassName");


String url = dbProps.getProperty("jdbc.url");


String username = dbProps.getProperty("jdbc.username");


String password = dbProps.getProperty("jdbc.password");




String initialSize = dbProps.getProperty("dataSource.initialSize");


String minIdle = dbProps.getProperty("dataSource.minIdle");


String maxIdle = dbProps.getProperty("dataSource.maxIdle");


String maxWait = dbProps.getProperty("dataSource.maxWait");


String maxActive = dbProps.getProperty("dataSource.maxActive");


//是否在自动回收超时连接的时候打印连接的超时错误


boolean logAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.logAbandoned","false"))).booleanValue();




//是否自动回收超时连接


boolean removeAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.removeAbandoned","false"))).booleanValue();




//超时时间(以秒数为单位)


int removeAbandonedTimeout = Integer.parseInt(dbProps.getProperty("dataSource.removeAbandonedTimeout","300"));




dataSource = new BasicDataSource();


dataSource.setDriverClassName(driveClassName);


dataSource.setUrl(url);


dataSource.setUsername(username);


dataSource.setPassword(password);




//初始化连接数


if(initialSize!=null)


dataSource.setInitialSize(Integer.parseInt(initialSize));




//最小空闲连接


if(minIdle!=null)


dataSource.setMinIdle(Integer.parseInt(minIdle));




//最大空闲连接


if(maxIdle!=null)


dataSource.setMaxIdle(Integer.parseInt(maxIdle));




//超时回收时间(以毫秒为单位)


if(maxWait!=null)


dataSource.setMaxWait(Long.parseLong(maxWait));




//最大连接数




if(maxActive!=null)...{


if(!maxActive.trim().equals("0"))


dataSource.setMaxActive(Integer.parseInt(maxActive));


}




System.out.println("logAbandoned="+logAbandoned);


dataSource.setLogAbandoned(logAbandoned);


dataSource.setRemoveAbandoned(removeAbandoned);


dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);




Connection conn = dataSource.getConnection();




if(conn==null)...{


log("创建连接池时,无法取得连接!检查设置!!!");




}else...{


conn.close();


}


System.out.println("连接池创建成功!!!");


}




catch (Exception e) ...{


e.printStackTrace();


System.out.println("创建连接池失败!请检查设置!!!");


}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: