Tomcat下配置Mysql数据库链接池(DBCP)

  
本文以 Tomcat5.0.28 Mysql 为环境来描述,其中 %Tomcat _Home% Tomcat 的安装目录, bookstore 为我的测试项目,放置在 %Tomcat _Home%/webapps
1 %Tomcat _Home%/conf 下找到 server.xml 文件,在 <Host></Host> 结点之间加入数据库链接池的详细信息,如下所示:
< Context  path ="/bookstore"  docBase ="bookstore"
        debug
="5"  reloadable ="true"  crossContext ="true" >

  
< Logger  className ="org.apache.catalina.logger.FileLogger"
             prefix
="localhost_bookstore_log."  suffix =".txt"
             timestamp
="true" />

  
< Resource  name ="jdbc/bookstore"
               auth
="Container"
               type
="javax.sql.DataSource" />

  
< ResourceParams  name ="jdbc/bookstore" >
    
< 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 -1 for no limit.  See also the DBCP documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         
-->
    
< 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 > root </ value >
    
</ parameter >
    
< parameter >
     
< name > password </ name >
     
< value > sa </ value >
    
</ parameter >

    
<!--  Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next
         if you want to use this driver - we recommend using Connector/J though
    <parameter>
       <name>driverClassName</name>
       <value>org.gjt.mm.mysql.Driver</value>
    </parameter>
     
-->
    
    
<!--  Class name for the official MySQL Connector/J driver  -->
    
< parameter >
       
< name > driverClassName </ name >
       
< value > com.mysql.jdbc.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://localhost:3306/bookstore?autoReconnect=true </ value >
    
</ parameter >
  
</ ResourceParams >
</ Context >
 
其中 bookstore 为你的 Web 项目,具体相关信息就是数据库连接池信息
2 、在 %Tomcat _Home%/common/lib bookstore/WEB-INF/lib 下加入 mysql jdbc 驱动,笔者所用为 mysql-connector-java-5.0.3-bin.jar
3 、修改 bookstore/WEB-INF 下的 web.xml 文件,如下所示:
<? xml version="1.0" encoding="ISO-8859-1" ?>
< web-app  xmlns ="http://java.sun.com/xml/ns/j2ee"
   xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version ="2.4" >
 
< description > MySQLDBCP </ description >
 
< resource-ref >
     
< description > DB Connection </ description >
     
< res-ref-name > jdbc/bookstore </ res-ref-name >
     
< res-type > javax.sql.DataSource </ res-type >
     
< res-auth > Container </ res-auth >
 
</ resource-ref >
</ web-app >

 

4 、在 bookstore 下建立测试文件, index.jsp ,关键代码如下所示:
<%
    Context ctx 
=   new  InitialContext();
    DataSource ds 
=  (DataSource)ctx.lookup( " java:comp/env/jdbc/bookstore " );
    Connection conn 
=  ds.getConnection();
。。。。。。。。
%>
 
5 、打开浏览器,输入 http://localhost:8088/bookstore/index.jsp ,即可输出其信息(其中 8088 为笔者的 tomcat 端口号)
 
具体信息可参考官方网站: http://tomcat.apache.org/tomcat-5.0-doc/jndi-datasource-examples-howto.html
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值