如何设置tomcat数据库连接池

本文详细介绍如何在Tomcat 4.X和5.X版本中配置Oracle和MySQL数据库连接池,包括配置步骤、参数说明及程序调用方式。

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

 

1如何设置TOMCAT数据库连接池
 
最近在网上,有很多朋友并不了解什么叫做数据库连接池,怎么配置连接池也不清楚。于是为了方便大家,我特此写这份文档。
以tomcat为例子:
如果tomcat版本为4.X版本
1) 设置oracle连接池(tomcat4.X)
在设置oracle连接池之前,要保证在%tomcathome%/common/lib下面有class12.jar这个oracle驱动程序。
在tomcat安装目录的conf文件夹里面有一个server.xml
打开它,你可以找到<Context path="" docBase="ROOT" debug="0"/>
将这段代码注释掉。然后再加上以下代码
<Context path="/oa" docBase="oa" debug="1" reloadable="true" crossContext="true">
  
  <Resource name="jdbc/orcldb" auth="Container" type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/orcldb">
  <parameter><name>factory</name><value>org.apache.commons.dbcp.BasicDataSourceFactory</value></parameter>
  <parameter><name>driverClassName</name><value>oracle.jdbc.driver.OracleDriver</value></parameter>
  <parameter><name>url</name><value>jdbc:oracle:thin:@localhost:1521:orcl</value></parameter>
  <parameter><name>username</name><value>user</value></parameter>
  <parameter><name>password</name><value>pwd</value></parameter>
  <parameter><name>maxActive</name><value>20</value></parameter>
  <parameter><name>maxIdle</name><value>10</value></parameter>
  <parameter><name>maxWait</name><value>10000</value></parameter>
  </ResourceParams>
  </Context>
在这里我一一说明:
path:你可以设置为"/your webApps name" 那么这样在浏览器里应该是http://localhost:8080/oa/才会到达首页。
      你可以设置为""(空)那么这样在浏览器里应该是http://localhost:8080/才会到达首页。
docBase:就是你的webApps名字。
debug:设置bug的级别0为最高级别。
reloadable:如果设置为true那么改了server.xml配置或者说改了web.xml配置或者说是改了.class以后不用重启tomcat他会自动装载。
Resource name及ResourceParams name 注意了:这个就是数据库连接池的名字。在程序之中所调用的池就是要调用它。
factory:一般是默认就可以了。
 driverClassName :oracle驱动程序名oracle.jdbc.driver.OracleDriver
url:jdbc:oracle:thin:@localhost:1521:orcl(oralce数据库连接字符串)
username:oracle用户名
password:oracle密码
maxActive maxIdle maxWait 自行设定,一般默认就可以了。

确定以后没有错以后进行下一步,要在WEB-INF里的web.xml里加上以下信息
<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 
<description>oralceDB</description> 
<resource-ref> 
<description>DB Connection</description> 
<res-ref-name>jdbc/orcldb</res-ref-name> 
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth> 
</resource-ref> 
</web-app>
那么程序之中如何调用这个连接池呢?如下==》
/**
     * 返回Oracle数据库连接 注意:红色的字体表示他要调用的连接池名,同配置
     * @return    Connection
     * @throws SQLException
     */
 public static synchronized Connection getOracleConn() throws SQLException {
  try {
   Context jndiCntx = new InitialContext();
      DataSource ds =
            (DataSource)jndiCntx.lookup( "java:comp/env/jdbc/orcldb");
   return ds.getConnection();
  } catch (NamingException ne) {
  
 
  
2如何设置TOMCAT数据库连接池
 
   throw new EJBException(ne);
  }
 }


2) 设置mysql连接池(tomcat4.X)
在设置mysql连接池之前,要保证在%tomcathome%/common/lib下面有mm.mysql-2.0.4-bin.jar这个mysql驱动程序。
在tomcat安装目录的conf文件夹里面有一个server.xml
打开它,你可以找到<Context path="" docBase="ROOT" debug="0"/>
将这段代码注释掉。然后再加上以下代码
<Context path="/oa" docBase="oa" debug="1" reloadable="true" crossContext="true">
  
  <Resource name="jdbc/mysqldb" auth="Container" type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/mysqldb">
  <parameter><name>factory</name><value>org.apache.commons.dbcp.BasicDataSourceFactory</value></parameter>
  <parameter><name>driverClassName</name><value>org.gjt.mm.mysql.Driver </value></parameter>
  <parameter><name>url</name><value>jdbc:mysql://192.168.1.10/news?useUnicode=true&characterEncoding=GBK </value></parameter>
  <parameter><name>username</name><value>user</value></parameter>
  <parameter><name>password</name><value>pwd</value></parameter>
  <parameter><name>maxActive</name><value>20</value></parameter>
  <parameter><name>maxIdle</name><value>10</value></parameter>
  <parameter><name>maxWait</name><value>10000</value></parameter>
  </ResourceParams>
  </Context>
在这里我一一说明:
path:你可以设置为"/your webApps name" 那么这样在浏览器里应该是http://localhost:8080/oa/才会到达首页。
      你可以设置为""(空)那么这样在浏览器里应该是http://localhost:8080/才会到达首页。
docBase:就是你的webApps名字。
debug:设置bug的级别0为最高级别。
reloadable:如果设置为true那么改了server.xml配置或者说改了web.xml配置或者说是改了.class以后不用重启tomcat他会自动装载。
Resource name及ResourceParams name 注意了:这个就是数据库连接池的名字。在程序之中所调用的池就是要调用它。
factory:一般是默认就可以了。
 driverClassName :mysql驱动程序名org.gjt.mm.mysql.Driver
url:jdbc:mysql://192.168.1.10/news?useUnicode=true&characterEncoding=GBK(mysql数据库连接字符串)
username:mysql用户名
password:mysql密码
maxActive maxIdle maxWait 自行设定,一般默认就可以了。

确定以后没有错以后进行下一步,要在WEB-INF里的web.xml里加上以下信息
<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 
<description>mysqlDB</description> 
<resource-ref> 
<description>DB Connection</description> 
<res-ref-name>jdbc/mysqldb</res-ref-name> 
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth> 
</resource-ref> 
</web-app>
那么程序之中如何调用这个连接池呢?如下==》
/**
     * 返回Mysql数据库连接 注意:红色的字体表示他要调用的连接池名,同配置
     * @return    Connection
     * @throws SQLException
     */
 public static synchronized Connection getMysqlConn() throws SQLException {
  try {
   Context jndiCntx = new InitialContext();
      DataSource ds =
            (DataSource)jndiCntx.lookup( "java:comp/env/jdbc/mysqldb");
   return ds.getConnection();
  } catch (NamingException ne) {
   throw new EJBException(ne);
  }
 }

如果tomcat版本为5.X版本
  
 
  
3如何设置TOMCAT数据库连接池
 
3) 设置oracle连接池(tomcat5.X)
在设置oracle连接池之前,要保证在%tomcathome%/common/lib下面有class12.jar这个oracle驱动程序。
在tomcat安装目录的/conf/Catalina/localhost文件夹里面建一个名为oracle.xml
然后加入以下代码:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="D:/oatomcat/webapps/oa" path="/" reloadable="true">
 <Resource name="jdbc/orcldb" 
 auth="Container" 
 type="javax.sql.DataSource"/> 
 
 <ResourceParams name="jdbc/orcldb"> 
 <parameter> 
 <name>factory</name> 
 <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> 
 </parameter> 
 
 <!-- Maximum number of dB connections in pool. Make sure you 
 configure your mysqld max_connections large enough to handle 
 all of your db connections. Set to 0 for no limit. 
 --> 
 <parameter> 
 <name>maxActive</name> 
 <value>100</value> 
 </parameter> 
 
 <!-- Maximum number of idle dB connections to retain in pool. 
 Set to 0 for no limit. 
 --> 
 <parameter> 
 <name>maxIdle</name> 
 <value>30</value> 
 </parameter> 
 
 <!-- Maximum time to wait for a dB connection to become available 
 in ms, in this example 10 seconds. An Exception is thrown if 
 this timeout is exceeded. Set to -1 to wait indefinitely. 
 --> 
 <parameter> 
 <name>maxWait</name> 
 <value>10000</value> 
 </parameter> 
 
 <!-- MySQL dB username and password for dB connections --> 
 <parameter> 
 <name>username</name> 
 <value>user</value> 
 </parameter> 
 <parameter> 
 <name>password</name> 
 <value>pwd</value> 
 </parameter> 
 
 <!-- Class name for mm.mysql JDBC driver --> 
 <parameter> 
 <name>driverClassName</name> 
 <value>oracle.jdbc.driver.OracleDriver</value>
 </parameter> 
  
 <!-- The JDBC connection url for connecting to your MySQL dB. 
 The autoReconnect=true argument to the url makes sure that the 
 mm.mysql JDBC Driver will automatically reconnect if mysqld closed the 
 connection. mysqld by default closes idle connections after 8 hours. 
 --> 
 <parameter> 
 <name>url</name> 
 <value>jdbc:oracle:thin:@192.168.3.82:1521:ORCL</value> 
 
 <!--must use & not use  & -->
 </parameter> 
 </ResourceParams> 

</Context>
在这里我一一说明:
path:你可以设置为"/" 那么这样在浏览器里应该是http://localhost:8080/oa/才会到达首页。
docBase:就是你的webApps名字。
debug:设置bug的级别0为最高级别。
reloadable:如果设置为true那么改了server.xml配置或者说改了web.xml配置或者说是改了.class以后不用重启tomcat他会自动装载。
Resource name及ResourceParams name 注意了:这个就是数据库连接池的名字。在程序之中所调用的池就是要调用它。
factory:一般是默认就可以了。
driverClassName :oracle驱动程序名oracle.jdbc.driver.OracleDriver
url:jdbc:oracle:thin:@192.168.3.82:1521:ORCL(oracle数据库连接字符串)
username:oracle用户名
password:oracle密码
maxActive maxIdle maxWait 自行设定,一般默认就可以了。

确定以后没有错以后进行下一步,要在WEB-INF里的web.xml里加上以下信息
<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd"> 
  
 
  
4如何设置TOMCAT数据库连接池
 
<web-app> 
<description>oracleDB</description> 
<resource-ref> 
<description>DB Connection</description> 
<res-ref-name>jdbc/orcldb</res-ref-name> 
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth> 
</resource-ref> 
</web-app>
那么程序之中如何调用这个连接池呢?如下==》
/**
     * 返回Oracle数据库连接 注意:红色的字体表示他要调用的连接池名,同配置
     * @return    Connection
     * @throws SQLException
     */
 public static synchronized Connection getOracleConn() throws SQLException {
  try {
   Context jndiCntx = new InitialContext();
      DataSource ds =
            (DataSource)jndiCntx.lookup( "java:comp/env/jdbc/orcldb");
   return ds.getConnection();
  } catch (NamingException ne) {
   throw new EJBException(ne);
  }
 }


4) 设置mysql连接池(tomcat5.X)
在设置mysql连接池之前,要保证在%tomcathome%/common/lib下面有mm.mysql-2.0.4-bin.jar这个mysql驱动程序。
在tomcat安装目录的/conf/Catalina/localhost文件夹里面建一个名为mysql.xml
然后加入以下代码:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="D:/oatomcat/webapps/oa" path="/" reloadable="true">
 <Resource name="jdbc/mysqldb" 
 auth="Container" 
 type="javax.sql.DataSource"/> 
 
 <ResourceParams name="jdbc/mysqldb"> 
 <parameter> 
 <name>factory</name> 
 <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> 
 </parameter> 
 
 <!-- Maximum number of dB connections in pool. Make sure you 
 configure your mysqld max_connections large enough to handle 
 all of your db connections. Set to 0 for no limit. 
 --> 
 <parameter> 
 <name>maxActive</name> 
 <value>100</value> 
 </parameter> 
 
 <!-- Maximum number of idle dB connections to retain in pool. 
 Set to 0 for no limit. 
 --> 
 <parameter> 
 <name>maxIdle</name> 
 <value>30</value> 
 </parameter> 
 
 <!-- Maximum time to wait for a dB connection to become available 
 in ms, in this example 10 seconds. An Exception is thrown if 
 this timeout is exceeded. Set to -1 to wait indefinitely. 
 --> 
 <parameter> 
 <name>maxWait</name> 
 <value>10000</value> 
 </parameter> 
 
 <!-- MySQL dB username and password for dB connections --> 
 <parameter> 
 <name>username</name> 
 <value>user</value> 
 </parameter> 
 <parameter> 
 <name>password</name> 
 <value>pwd</value> 
 </parameter> 
 
 <!-- Class name for mm.mysql JDBC driver --> 
 <parameter> 
 <name>driverClassName</name> 
 <value>org.gjt.mm.mysql.Driver</value>
 </parameter> 
  
 <!-- The JDBC connection url for connecting to your MySQL dB. 
 The autoReconnect=true argument to the url makes sure that the 
 mm.mysql JDBC Driver will automatically reconnect if mysqld closed the 
 connection. mysqld by default closes idle connections after 8 hours. 
 --> 
 <parameter> 
 <name>url</name> 
 <value>jdbc:mysql://192.168.1.10/news?useUnicode=true&characterEncoding=GBK </value> 
 
 <!--must use & not use  & -->
 </parameter> 
 </ResourceParams> 

</Context>
在这里我一一说明:
path:你可以设置为"/" 那么这样在浏览器里应该是http://localhost:8080/oa/才会到达首页。
docBase:就是你的webApps路径和名字(D:/oatomcat/webapps/oa)。
debug:设置bug的级别0为最高级别。
reloadable:如果设置为true那么改了server.xml配置或者说改了web.xml配置或者说是改了.class以后不用重启tomcat他会自动装载。
Resource name及ResourceParams name 注意了:这个就是数据库连接池的名字。在程序之中所调用的池就是要调用它。
factory:一般是默认就可以了。
 driverClassName :mysql驱动程序名org.gjt.mm.mysql.Driver
url:jdbc:mysql://192.168.1.10/news?useUnicode=true&characterEncoding=GBK(mysql数据库连接字符串)
username:mysql用户名
password:mysql密码
maxActive maxIdle maxWait 自行设定,一般默认就可以了。

确定以后没有错以后进行下一步,要在WEB-INF里的web.xml里加上以下信息
<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 
<description>mysqlDB</description> 
<resource-ref> 
<description>DB Connection</description> 
<res-ref-name>jdbc/mysqldb</res-ref-name> 
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth> 
</resource-ref> 
</web-app>
那么程序之中如何调用这个连接池呢?如下==》
/**
     * 返回Mysql数据库连接 注意:红色的字体表示他要调用的连接池名,同配置
     * @return    Connection
     * @throws SQLException
     */
 public static synchronized Connection getMysqlConn() throws SQLException {
  try {
   Context jndiCntx = new InitialContext();
      DataSource ds =
            (DataSource)jndiCntx.lookup( "java:comp/env/jdbc/mysqldb");
   return ds.getConnection();
  } catch (NamingException ne) {
   throw new EJBException(ne);
  }
 } 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值