JNDI数据源(在Tomcat下配置JNDI多数据源实例)

一,添加数据库驱动包加入classpath。

这里我用到了oracle和mysql。
所以由两个jar包:ojdbc14.jar和mysql-connector-java-5.1.13-bin.jar。


(有的也说需要添加commons-dbcp-1.4.jar,commons-pool-1.5.4.jar和commons-collections.jar,
我做了测试,不用的。
网上查了下,DBCP使用Jakarta-Commons Database Connection Pool,它依赖以下三个包:
Jakarta-Commons DBCP,Jakarta-Commons Collections,Jakarta-Commons Pool。
在Tomcat的安装目录提供了一个集成的jar包 $CATALINA_HOME/lib/tomcat-dbcp.jar。
所以有这个tomcat-dbcp.jar就可以了。


二,修改Tomcat_Home/conf/server.xml,在GlobalNamingResources中加入:

<Resource name="jdbc/cdbank" auth="Container" 
     type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"  
     url="jdbc:oracle:thin:@10.55.15.66:1521:cdbank"  
     username="ccdb" password="ccdb" maxActive="10" maxIdle="5"  
     maxWait="-1"/>

<Resource name="jdbc/mydb" auth="Container" 
     type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"  
     url="jdbc:mysql://127.0.0.1:3306/mydb"  
     username="root" password="root" maxActive="10" maxIdle="5"  
     maxWait="-1"/>

属性说明:
name:数据源的名称。


auth:指定管理Resource的Manager,由两个可选值:Container和Application。
Container表示由容器来创建和管理Resource。
Application表示由WEB应用来创建和管理Resource。
如果在web application deployment descriptor中使用<resource-ref>,这个属性是必需的。
如果使用<resource-env-ref>,这个属性是可选的。


type:指定Resource所属的java类名:javax.sql.DataSource。

maxActive: 指定数据库连接池中处于活动状态的数据库连接最大数目,0表示不受限制 

maxIdle: 指定数据库连接池中处于空闲状态的数据库连接的最大数目,0表示不受限制 

maxWait: 指定数据库连接池中的数据库连接处于空闲状态的最长时间(单位为毫秒),超过这一事件,
将会抛出异常。-1表示可以无限期等待。 

username: 连接数据库的用户名 

password: 连接数据库的密码 

driverClassName: 指定连接数据库的JDBC驱动程序。
(oracle驱动:oracle.jdbc.OracleDriver
 mysql驱动: com.mysql.jdbc.Driver)

url:连接数据库的URL


完整server.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.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8006" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users-->
    
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值