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

 1、tomcat/conf/context.xml中进行如下配置。调用时,通过JNDI名称进行调用。

或者在:tomcat/conf/server.xml中的 <GlobalNamingResources>下进行如下配置

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

<?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是作为所有应用的通用配置来进行配置的,故需要谨慎配置,否则会导致数据库的连接池浪费以及堵塞。

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

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

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

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

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

发布了129 篇原创文章 · 获赞 20 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/Bof_jangle/article/details/96479675
今日推荐