Jndi and c3p0 in Tomcat

Tomcat 中Jndi是使用Tomcat自带的连接池
抛弃Tomcat自带的连接池。使用c3p0 。

环境:Tomcat 5.5.20
下面配置只适合Tomcat 5.5.X


下面来看Jndi 与 c3p0 结合:
c3p0 下载地址: http://nchc.dl.sourceforge.net/sourceforge/c3p0/c3p0-0.9.0.4.bin.zip
把 c3p0-0.9.0.4.jar 放到应用服务的WEB-INF/lib目录下。如:DBTest/WEB-INF/lib

1.在server.xml中<Context></Context>内加<Resource />,完整的示例:
< Context  path ="/DBTest"  docBase ="DBTest"
        debug
="5"  reloadable ="true"  crossContext ="true" >
< Resource  auth ="Container"
 driverClass
="com.mysql.jdbc.Driver"
                                    maxPoolSize
="50"  minPoolSize ="2"  acquireIncrement ="2"
                                    name
="jdbc/connPool"  user ="root"  password =""
                                    factory
="org.apache.naming.factory.BeanFactory"
                                    type
="com.mchange.v2.c3p0.ComboPooledDataSource"
                                    jdbcUrl
="jdbc:mysql://localhost:3306/test"   />
</ Context >


2.在web.xml添加:
   < resource-ref >
      
< description > DB Connection </ description >
      
< res-ref-name > jdbc/connPool </ res-ref-name >
      
< res-type > javax.sql.DataSource </ res-type >
      
< res-auth > Container </ res-auth >
  
</ resource-ref >

3.测试页面testConnPool.jsp
<% @ page language = " java "  pageEncoding = " UTF-8 " %>
<% @ taglib uri = " http://java.sun.com/jsp/jstl/sql "  prefix = " sql "   %>
<% @ taglib uri = " http://java.sun.com/jsp/jstl/core "  prefix = " c "   %>

< sql:query  var ="rs"  dataSource ="jdbc/connPool" >
select id, typename from text
</ sql:query >

< html >
  
< head >
    
< title > DB Test Conn Pool c3p0 and Jndi </ title >
  
</ head >
  
< body >

  
< h2 > Results </ h2 >
  
< c:forEach  var ="row"  items ="${rs.rows}" >
    id: ${row.id}
< br />
    name: ${row.typename}
< br />
</ c:forEach >

  
</ body >
</ html >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值