tomcat中应用JNDI配置DBCP连接池。

13 篇文章 0 订阅
3 篇文章 0 订阅

目录

 一、tomcat/conf/context.xml中

二、在tomcat/conf/server.xml中的 GlobalNamingResources 下

三、总结:


本文是对DBCP配置参数的说明与记录。

 一、tomcat/conf/context.xml中

进行如下配置。调用时,通过JNDI名称进行调用。

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->
<!-- 使用DBCP配置针对MySQL数据库的JNDI数据源 -->
	 <Resource name="jndi/demo"		//jndi的名称   
		auth="Container"   
		type="javax.sql.DataSource"   //数据源类型,使用标准的javax.sql.DataSource
		driverClassName="com.mysql.jdbc.Driver"   //jdbc驱动类型 这里是MySQL 也可以是Oracle等。
		url="jdbc:mysql://127.0.0.1:3306/demo"    //URL地址
		username="jangle"   
		password="1"   
		initialSize="50"		//初始化时,创建的连接数。-- 连接数在开始使用后会缓慢增加至最小空闲连接数。
		maxActive="-1"		//最大活动连接数		-- 限制最大连接数 设置为-1则不限制连接数
		maxIdle="200"		 //最大空闲连接数	-- 当并发量高的时候,连接数量可能超过200,但不会超过maxActive。超过maxIdle的连接,在使用完毕后会立即销毁
		minIdle="100"		 //最小空闲连接数	-- 当业务冷清,没有访问量时,回收器会对连接进行销毁,但会保障存活minIdle设置的数量。
		maxWait="4000"	//当池的数据库连接已经被占用的时候,最大等待时间。单位是秒
		testWhileIdle="true"	// 由空闲连接回收器对其进行检测。检测失败(即连接不可用)则将其从连接池中移除。	
			timeBetweenEvictionRunsMillis="30000"	//空闲连接回收器的执行频率。单位毫秒,即每30秒执行一次。
			numTestsPerEvictionRun="10"	// 每次空闲连接回收器检测的数量。 即每30秒检测一次,1次检测10条。
			validationQuery="select 1 from dual"	//验证连接是否可用的sql语句。 即,检测规则是执行这个sql,如果执行失败,则从池中移除。
		minEvictableIdleTimeMillis = "1800000"  //空闲连接存活时长,单位毫秒,即连接空闲30分钟后,被释放回收。 -- 但空闲连接会一直保持大于minIdle的数量。
		testOnBorrow="false"	// 池中连接被取出使用时,不进行可用性测试。否则影响性能。
		testOnReturn="false"	// 使用完连接返回池中时,不进行可用性测试。否则影响性能。	  
		validationQueryTimeout="1"
		
		removeAbandoned="true"  // 连接泄漏回收参数,当可用连接数少于3个时才执行。 -- 即去检查,是否有某个连接取出后,未进行任何操作,当扫描时,发现他未活动时间超过removeAbandonedTimeout设置的时长时,会被回收。  
		removeAbandonedTimeout="180"  // 连接泄漏回收参数,180秒,泄露的连接可以被删除的超时值。单位秒。
                                                />
<!-- 使用C3P0配置针对MySQL数据库的JNDI数据源 -->
      <Resource 
		name="jdbc/MysqlDataSource" 
		auth="Container"
		factory="org.apache.naming.factory.BeanFactory" 	//新增了工厂 factory
		type="com.mchange.v2.c3p0.ComboPooledDataSource"	//type值修改为c3p0的数据源类型
		driverClass="com.mysql.jdbc.Driver"		// 跟dbcp的区别是这里 driverClassName -》driverClass
		maxIdle="700"
		minIdle="200"
		maxActive="700"
		idleConnectionTestPeriod="60"
		maxPoolSize="50" 
		minPoolSize="2"
		acquireIncrement="2" 
		user="root" 			// 跟dbcp的区别是这里 username -》 user
		password="root"
		jdbcUrl="jdbc:mysql://127.0.0.1:3306/demo"/>	//跟dbcp的区别是这里 url -》 jdbcUrl
</Context>

maxIdle值与maxActive值应配置的接近。

因为,当连接数超过maxIdle值后,刚刚使用完的连接(刚刚空闲下来)会立即被销毁。而不是我想要的空闲M秒后再销毁起一个缓冲作用。这一点DBCP做的可能与你想像的不一样。

若maxIdle与maxActive相差较大,在高负载的系统中会导致频繁的创建、销毁连接,连接数在maxIdle与maxActive间快速频繁波动,这不是我想要的。

高负载系统的maxIdle值可以设置为与maxActive相同或设置为-1(-1表示不限制),让连接数量在minIdle与maxIdle间缓冲慢速波动。

注意事项:

context.xml是作为Context配置的默认配置(Catalina容器级默认配置),即每个Context进行初始化时(不存在Host级默认配置和configFile的情况下),context.xml的配置内容会被每个Context继承,故会拥有多分拷贝。

所以,context.xml是作为所有应用的通用配置来进行配置的,故需要谨慎配置,否则会导致数据库的连接池浪费以及堵塞。

二、在tomcat/conf/server.xml中的 GlobalNamingResources 下

进行如下配置

<GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
	<Resource name="jndi/demo"		 
		auth="Container"   
		type="javax.sql.DataSource"    
		driverClassName="com.mysql.jdbc.Driver"    
		url="jdbc:mysql://mysql.jangle.xyz:10071/demo"    
		username="jangle"   
		password="123"   
		initialSize="5"		 
		maxActive="-1"		 
		maxIdle="20"		 
		minIdle="10"		  
		maxWait="4000"	 
		testWhileIdle="true"	 
			timeBetweenEvictionRunsMillis="30000"	 
			numTestsPerEvictionRun="10"	 
			validationQuery="select 1 from dual" 
		minEvictableIdleTimeMillis = "1800000"  
		testOnBorrow="false"	
		testOnReturn="false"		  
		validationQueryTimeout="1"
		removeAbandoned="true"    
		removeAbandonedTimeout="180"  
	/>
  </GlobalNamingResources>

然后在context.xml添加ResourceLink配置。

<?xml version="1.0" encoding="UTF-8"?>

<Context>
    
    <ResourceLink global="jndi/demo" name="jndi/demo" type="javax.sql.DataSource" />  
                             
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

   
</Context>

这样配置,所有应用共享一个全局配置。

注意事项:因为在server.xml中配置全局的数据源,故是在tomcat启动时就进行加载的,所以要添加jndi对应服务的jar包。此处我们配置的连接池使用的是MySQL的驱动,故在要tomcat的lib下添加mysql驱动jar包(mysql-connector-java-XX.jar)。(容器的启动时无法读取应用下的lib,参见Tomcat类加载器的实现)

 

三、总结:

(在数据库能正常提供服务的前提下)

1、context.xml的单独配置(20个单独的连接,5个应用):所有应用单独使用自己的配置,负载能力有限,但能保证自己提供有限的服务,不受其他应用影响。

2、server.xml全局配置(100个共享连接):所有应用共享,增加负载能力,但会因为其中一个应用访问量大,导致其他应用无法访问。

两种方式各有利弊,按需配置(不管如何配置,他们最终依然会受制于数据库提供连接池服务的能力。)

 

DBCP与C3P0的区别:

参考文献:https://elf8848.iteye.com/blog/1931778

https://blog.csdn.net/meandmyself/article/details/49863225 removeAbandoned的说明

https://baike.baidu.com/item/c3p0/3719378?fr=aladdin

https://www.cnblogs.com/xdp-gacl/p/4040103.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值