使用JNDI进行Tomcat 5.5.17和MySQL连接池的配置

本文介绍如何在Tomcat服务器中配置JNDI数据源,包括修改server.xml文件、创建项目对应的XML文件以及调整web.xml文件的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明一下步骤:

一、修改server.xml文件

      <Resource
      name="jdbc/testA"
      type="javax.sql.DataSource"
	auth="Container"
      password="123456"
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="2"
      maxWait="5000"
      username="root"
      url="jdbc:mysql://localhost:3306/testA"
      maxActive="4"/>
      <Resource
      name="jdbc/testB"
      type="javax.sql.DataSource"
	auth="Container"
      password="123456"
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="2"
      maxWait="5000"
      username="root"
      url="jdbc:mysql://localhost:3306/testB"
      maxActive="4"/>
PS.有多个JNDI就添加多个

二、在%TOMCAT_HOME%/conf/Catalina/localhost下新建一个与你项目文件夹同名的xml文件

例如:我的项目名为testJNDI,那就创建一个testJNDI.xml的文件。内容为:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
     <Resource
      name="jdbc/testA"
      type="javax.sql.DataSource"
	auth="Container"
      password="123456"
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="2"
      maxWait="5000"
      username="root"
      url="jdbc:mysql://localhost:3306/testA"
      maxActive="4"/>
      <Resource
      name="jdbc/testB"
      type="javax.sql.DataSource"
	auth="Container"
      password="123456"
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="2"
      maxWait="5000"
      username="root"
      url="jdbc:mysql://localhost:3306/testB"
      maxActive="4"/>
</Context>
PS.其实Resource的内容和server.xml的一致。注意,要是这错误的话,会抛出错误
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' 

三、修改%TOMCAT_HOME%/webapps/yourweb/WEB-INF下的web.xml文件

 在</web-app>之前添加:

<resource-ref>

    <description>mysql JNDI</description>

    <res-ref-name>jdbc/testA</res-ref-name>

    <res-type>javax.sql.DataSource</res-type>

    <res-auth>Container</res-auth>

  </resource-ref>

<resource-ref>

    <description>mysql JNDI</description>

    <res-ref-name>jdbc/testA</res-ref-name>

    <res-type>javax.sql.DataSource</res-type>

    <res-auth>Container</res-auth>

  </resource-ref>

PS.有多个就添加多个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值