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

 

在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

你可能感兴趣的:(jdbc)