在Tomcat7中JNDI方式使用tomcat-jdbc连接池

14 篇文章 0 订阅
9 篇文章 0 订阅

 

在tomcat中,jndi方式默认使用的是tomcat-dbcp连接池,这个已经广受诟病,而tomcat-jdbc因其优异的性能,现正逐步取代dbcp,今天我们就讲讲在tomcat7中如何配置:

 

在作如下配置之前,请先将对应数据库的JDBC驱动包放到tomcat/lib 目录中,避免启动时找不到驱动.

 

1 修改tomcat/conf/context.xml文件,添加如下内容:

<?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>

     
   <Resource name="jdbc/pdcourt"
      auth="Container"
      type="javax.sql.DataSource"
      factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
      testWhileIdle="true"
      testOnBorrow="true"
      testOnReturn="false"
      validationQuery="SELECT 1 from dual"
      validationInterval="3000"
      timeBetweenEvictionRunsMillis="3000"
      maxActive="300"
      minIdle="10"
      maxWait="10000"
      initialSize="10"
      removeAbandonedTimeout="60"
      removeAbandoned="true"
      logAbandoned="true"
      minEvictableIdleTimeMillis="30000"
      jmxEnabled="true"
      jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
      driverClassName="oracle.jdbc.OracleDriver"
      url="jdbc:oracle:thin:@127.0.0.1:1521:test"
      username="test" 
      password="test"/>
</Context>

 

2 在应用的web.xml文件中添加如下:

	<resource-ref>
		<!-- 描述信息 -->
		<description>Connection Pool</description>
		<!-- 数据源名字 和上面配置中数据源的名字一致 -->
		<res-ref-name>jdbc/test</res-ref-name>
		<!-- 数据源类型 -->
		<res-type>javax.sql.PooledConnection</res-type>
		<res-auth>Container</res-auth>
		<res-sharing-scope>Shareable</res-sharing-scope>
	</resource-ref>

 

3 如果是使用Spring,则修改原数据库连接池

	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>java:comp/env/jdbc/test</value>
		</property>
	</bean>

 

4 其它的使用方式可以在网上搜索,通过配置后,就可以把数据库连接池迁移到tomcat上管理

  当然,也可以把连接池配置到应用中.

 

 

参考:

http://kingxss.iteye.com/blog/1481872

http://www.cnblogs.com/lihuiyy/archive/2012/02/14/2351768.html

http://www.oschina.net/question/12_36910

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值