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

相关文章
相关标签/搜索