commons DBCP 配置參數簡要說明
前段時間因為項目原因,要在修改數據庫連接池到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("創建連接池失敗!請檢查設置!!!");
??????? }
有使用問題或建議可與我聯系:yy-man@163.com
??????
?????????2006-04-20?? By: 小土