`
zhangxiong0301
  • 浏览: 352322 次
社区版块
存档分类
最新评论

tomcat dbcp 参数配置项(转载)

    博客分类:
  • JAVA
阅读更多
参数


描述

username


传递给JDBC驱动的用于建立连接的用户名

password


传递给JDBC驱动的用于建立连接的密码

url


传递给JDBC驱动的用于建立连接的URL

driverClassName


使用的JDBC驱动的完整有效的java 类名

connectionProperties


当建立新连接时被发送给JDBC驱动的连接参数,格式必须是 [propertyName=property;]*
注意:参数user/password将被明确传递,所以不需要包括在这里。



参数


默认值


描述

defaultAutoCommit


true


连接池创建的连接的默认的auto-commit状态

defaultReadOnly


driver default


连接池创建的连接的默认的read-only状态. 如果没有设置则setReadOnly方法将不会被调用. (某些驱动不支持只读模式,比如:Informix)

defaultTransactionIsolation


driver default


连接池创建的连接的默认的TransactionIsolation状态. 下面列表当中的某一个: (参考javadoc)

    NONE
    READ_COMMITTED
    READ_UNCOMMITTED
    REPEATABLE_READ
    SERIALIZABLE

defaultCatalog





连接池创建的连接的默认的catalog



参数


默认值


描述

initialSize


0


初始化连接:连接池启动时创建的初始化连接数量,1.2版本后支持

maxActive


8


最大活动连接:连接池在同一时间能够分配的最大活动连接的数量, 如果设置为非正数则表示不限制

maxIdle


8


最大空闲连接:连接池中容许保持空闲状态的最大连接数量,超过的空闲连接将被释放,如果设置为负数表示不限制

minIdle


0


最小空闲连接:连接池中容许保持空闲状态的最小连接数量,低于这个数量将创建新的连接,如果设置为0则不创建

maxWait


无限


最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间(以毫秒计数),超过时间则抛出异常,如果设置为-1表示无限等待



参数


默认值


描述

validationQuery





SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前.如果指定,则查询必须是一个SQL SELECT并且必须返回至少一行记录

testOnBorrow


true


指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个.
注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串

testOnReturn


false


指明是否在归还到池中前进行检验
注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串

testWhileIdle


false


指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.
注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串

timeBetweenEvictionRunsMillis


-1


在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. 如果设置为非正数,则不运行空闲连接回收器线程

numTestsPerEvictionRun


3


在每次空闲连接回收器线程(如果有)运行时检查的连接数量

minEvictableIdleTimeMillis


1000 * 60 * 30


连接在池中保持空闲而不被空闲连接回收器线程(如果有)回收的最小时间值,单位毫秒



参数


默认值


描述

poolPreparedStatements


false


开启池的prepared statement 池功能

maxOpenPreparedStatements


不限制


statement池能够同时分配的打开的statements的最大数量, 如果设置为0表示不限制


这里可以开启PreparedStatements池. 当开启时, 将为每个连接创建一个statement池,并且被下面方法创建的PreparedStatements将被缓存起来:
    * public PreparedStatement prepareStatement(String sql)
    * public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
注意: 确认连接还有剩余资源可以留给其他statement

参数


默认值


描述

accessToUnderlyingConnectionAllowed


false


控制PoolGuard是否容许获取底层连接


如果容许则可以使用下面的方式来获取底层连接:
    Connection conn = ds.getConnection();
    Connection dconn = ((DelegatingConnection) conn).getInnermostDelegate();
    ...
    conn.close();

默认false不开启, 这是一个有潜在危险的功能, 不适当的编码会造成伤害.(关闭底层连接或者在守护连接已经关闭的情况下继续使用它).请谨慎使用,并且仅当需要直接访问驱动的特定功能时使用.
注意: 不要关闭底层连接, 只能关闭前面的那个.

参数


默认值


描述

removeAbandoned


false


标 记是否删除泄露的连接,如果他们超过了removeAbandonedTimout的限制.如果设置为true, 连接被认为是被泄露并且可以被删除,如果空闲时间超过removeAbandonedTimeout. 设置为true可以为写法糟糕的没有关闭连接的程序修复数据库连接.

removeAbandonedTimeout


300


泄露的连接可以被删除的超时值, 单位秒

logAbandoned


false


标记当Statement或连接被泄露时是否打印程序的stack traces日志。被泄露的Statements和连接的日志添加在每个连接打开或者生成新的Statement,因为需要生成stack trace。


如果开启"removeAbandoned",那么连接在被认为泄露时可能被池回收. 这个机制在(getNumIdle() < 2) and (getNumActive() > getMaxActive() - 3)时被触发.
举例当maxActive=20, 活动连接为18,空闲连接为1时可以触发"removeAbandoned".但是活动连接只有在没有被使用的时间超过"removeAbandonedTimeout"时才被删除,默认300秒.在resultset中游历不被计算为被使用.





 在配置时,主要难以理解的主要有: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

#超时时间(以秒数为单位)
#设置超时时间有一个要注意的地方,超时时间=现在的时间-程序中创建Connection的时间,如果 maxActive比较大,比如超过100,那么removeAbandonedTimeout可以设置长一点比如180,也就是三分钟无响应的连接进行 回收,当然应用的不同设置长度也不同。
dataSource.removeAbandonedTimeout=180

#<!-- 超时等待时间以毫秒为单位 -->
#maxWait代表当Connection用尽了,多久之后进行回收丢失连接
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.error("创建连接池时,无法取得连接!检查设置!!!");
         }else{
          conn.close();
         }
         log.error("连接池创建成功!!!");
        }
        catch (Exception e) {
              log.error("创建连接池失败!请检查设置!!!");
        }

有误的地方欢迎指正!      by yy。。。。。。



Hibernate支持DBCP包进行数据库连接池的配置。简要说明配置过程:
(仅仅是个人摸索,如有问题,欢迎指点)
需要的包:
Commons-Pool (下载地址:http://jakarta.apache.org/commons/pool/) :提供一个一般对象池的接口,而且包含实现了通常池工具箱。
Commons-DBCP(下载地址:http://jakarta.apache.org/commons/dbcp/) :提供数据库连接池服务。DBCP很聪明,把Commons-Pool和JDBC的driver封装起来,使达到连接池的目的。

Hibernate属性文件的配置参数
#连接池的最大活动个数
hibernate.dbcp.maxActive 100
#当连接池中的连接已经被耗尽的时候,DBCP将怎样处理( 0 = 失败, 1 = 等待, 2= 增长)
hibernate.dbcp.whenExhaustedAction 1
#最大等待时间
hibernate.dbcp.maxWait 120000
#没有人用连接的时候,最大闲置的连接个数。
hibernate.dbcp.maxIdle 10
##以下是对prepared statement的处理,同上。
hibernate.dbcp.ps.maxActive 100
hibernate.dbcp.ps.whenExhaustedAction 1
hibernate.dbcp.ps.maxWait 120000
hibernate.dbcp.ps.maxIdle 10

## 可选,是否对池化的连接进行验证
#给出一条简单的sql语句进行验证
#hibernate.dbcp.validationQuery select 1 from dual
#在取出连接时进行有效验证
#hibernate.dbcp.testOnBorrow true
#在放回连接时进行有效验证
#hibernate.dbcp.testOnReturn false

#Hibernate已经实现了DBCP Provider实现,别忘了在下面的键值去掉#字符
hibernate.connection.provider_class net.sf.hibernate.connection.DBCPConnectionProvider



    <property name="connection.pool.size">2</property> 
    <property name="statement_cache.size">25</property> 
    <property name="jdbc.fetch_size">50</property> 
    <property name="jdbc.batch_size">30</property> 
     
    <property name="show_sql">true</property> 
    <property name="connection.provider_class">net.sf.hibernate.connection.DBCPConnectionProvider</property> 
    <property name="dbcp.maxActive">100</property> 
    <property name="dbcp.whenExhaustedAction">1</property> 
    <property name="dbcp.maxWait">120000</property> 
    <property name="dbcp.maxIdle">10</property> 
    <property name="dbcp.ps.maxActive">100</property> 
    <property name="dbcp.ps.whenExhaustedAction">1</property> 
    <property name="dbcp.ps.maxWait">120000</property> 
    <property name="dbcp.ps.maxIdle">100</property> 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics