BasicDataSource配備

BasicDataSource配置html

 

commons DBCP 配置參數簡要說明 java

  前段時間由於項目緣由,要在修改數據庫鏈接池到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=passsql

#<!-- 初始化鏈接 -->
dataSource.initialSize=10數據庫

#<!-- 最大空閒鏈接 -->
dataSource.maxIdle=20apache

#<!-- 最小空閒鏈接 -->
dataSource.minIdle=5oracle

#最大鏈接數量
dataSource.maxActive=50app

#是否在自動回收超時鏈接的時候打印鏈接的超時錯誤
dataSource.logAbandoned=trueurl

#是否自動回收超時鏈接
dataSource.removeAbandoned=truespa

#超時時間(以秒數爲單位)
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: 小土

 

 

 

用apache的dbcp來創建獨立的數據庫鏈接池(db connection pool)
數據庫鏈接池的好處是不言而喻的,如今大部分的application server都提供本身的數據庫鏈接池方案,此時,只要按照application server的文檔說明,正確配置,便可在應用中享受到數據庫鏈接池的好處。
但 是,有些時候,咱們的應用是個獨立的java application,並非普通的WEB/J2EE應用,並且是單獨運行的,不要什麼application server的配合,這種狀況下,咱們就須要創建本身的數據庫鏈接池方案了。這裏,介紹如何利用apache的dbcp來創建爲民本身的數據庫鏈接池。
1。首先,下載必須的jar包
dbcp包,目前版本是1.2.1:http://jakarta.apache.org/commons/dbcp/ 
pool包,目前版本是1.3:http://jakarta.apache.org/commons/pool/, 
若是下載的pool包是1.2的版本,還要下載common-collections包:http://jakarta.apache.org/commons/collections/
在創建咱們本身的數據庫鏈接池時,可使用xml文件來傳入須要的參數,這裏只使用hard code的方式來簡單介紹,全部須要咱們本身寫的代碼不多,只要創建一個文件以下:

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import java.sql.SQLException;
import java.sql.Connection;
import java.util.Properties;

public class ConnectionSource {
    private static BasicDataSource dataSource = null;

    public ConnectionSource() {
    }

    public static void init() {

        if (dataSource != null) {
            try {
                dataSource.close();
            } catch (Exception e) {
                //
            }
            dataSource = null;
        }

        try {
            Properties p = new Properties();
            p.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver");
            p.setProperty("url", "jdbc:oracle:thin:@192.168.0.1:1521:testDB");
            p.setProperty("password", "scott");
            p.setProperty("username", "tiger");
            p.setProperty("maxActive", "30");
            p.setProperty("maxIdle", "10");
            p.setProperty("maxWait", "1000");
            p.setProperty("removeAbandoned", "false");
            p.setProperty("removeAbandonedTimeout", "120");
            p.setProperty("testOnBorrow", "true");
            p.setProperty("logAbandoned", "true");

            dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p);

        } catch (Exception e) {
            //
        }
    }


    public static synchronized Connection getConnection() throws  SQLException {
        if (dataSource == null) {
            init();
        }
        Connection conn = null;
        if (dataSource != null) {
            conn = dataSource.getConnection();
        }
        return conn;
    }
}

接下來,在咱們的應用中,只要簡單地使用 ConnectionSource.getConnection()就能夠取得鏈接池中的數據庫鏈接,享受數據庫鏈接帶給咱們的好處了。當咱們使用完取得 的數據庫鏈接後,只要簡單地使用connection.close()就可把此鏈接返回到鏈接池中,至於爲何不是直接關閉此鏈接,而是返回給鏈接池,這 是由於dbcp使用委派模型來實現Connection接口了。

在使用Properties來建立BasicDataSource時,有不少參數能夠設置,比較重要的還有:

testOnBorrow、testOnReturn、testWhileIdle,他們的意思是當 是取得鏈接、返回鏈接或鏈接空閒時是否進行有效性驗證(便是否還和數據庫連通的),默認都爲false。因此當數據庫鏈接由於某種緣由斷掉後,再從鏈接池 中取得的鏈接,實際上多是無效的鏈接了,因此,爲了確保取得的鏈接是有效的, 能夠把把這些屬性設爲true。當進行校驗時,須要另外一個參數:validationQuery,對oracle來講,能夠是:SELECT COUNT(*) FROM DUAL,實際上就是個簡單的SQL語句,驗證時,就是把這個SQL語句在數據庫上跑一下而已,若是鏈接正常的,固然就有結果返回了。

還有2個參數:timeBetweenEvictionRunsMillis 和 minEvictableIdleTimeMillis, 他們兩個配合,能夠持續更新鏈接池中的鏈接對象,當timeBetweenEvictionRunsMillis 大於0時,每過timeBetweenEvictionRunsMillis 時間,就會啓動一個線程,校驗鏈接池中閒置時間超過minEvictableIdleTimeMillis的鏈接對象。

本文轉載:http://www.myexception.cn/database/618357.html

相關文章
相關標籤/搜索