Tomcat配置c3p0数据源

使用C3P0配置JNDI数据源
Tomcat6.x中配置JNDI数据源时默认使用的是Tomcat6.x自带的DBCP连接池,Tomcat6.x使用DBCP连接池配置JNDI数据源如下:
 <Resource 
      name="oracleDataSource" 
      auth="Container" 
      type="javax.sql.DataSource" 
      maxActive="50" 
      maxIdle="10" 
      maxWait="10000" 
      username="lead_oams" 
      password="p" 
      driverClassName="oracle.jdbc.OracleDriver" 
      url="jdbc:oracle:thin:@192.168.1.229:1521:lead"/>


  如果想让Tomcat6.x使用C3P0连接池配置JNDI数据源,在配置时,以下配置项需要修改
   1、 type和factory的值发生变化
   2、username=>user
   3、url=>jdbcUrl
   4、driverClassName=>driverClass
  创建一个Web测试项目C3P0_JNDI_Config,解压压缩包,找到c3p0-0.9.5-pre9\lib目录下的相关Jar包.
  
  将C3P0的相关Jar包添加到项目中,在项目的META-INF目录下创建一个context.xml文件.
  
  在tomcat服务器的lib目录下添加Oracle、MySQL、SQLServer三种数据库的驱动jar包.
  
  1、在context.xml文件中加入如下配置信息


 <Context>
      <!-- 使用C3P0配置针对MySQL数据库的JNDI数据源 -->
      <Resource 
          name="jdbc/MysqlDataSource" 
          auth="Container"
          factory="org.apache.naming.factory.BeanFactory" 
          type="com.mchange.v2.c3p0.ComboPooledDataSource"
          driverClass="com.mysql.jdbc.Driver"
          idleConnectionTestPeriod="60"
          maxPoolSize="50" 
          minPoolSize="2"
          acquireIncrement="2" 
          user="root" 
          password="root"
          jdbcUrl="jdbc:mysql://192.168.1.144:3306/leadtest"/>
         
     <!-- 使用C3P0配置针对Oracle数据库的JNDI数据源 -->
     <Resource 
         name="jdbc/OracleDataSource" 
         auth="Container"
         factory="org.apache.naming.factory.BeanFactory" 
         type="com.mchange.v2.c3p0.ComboPooledDataSource"
         driverClass="oracle.jdbc.OracleDriver"
         idleConnectionTestPeriod="60"
         maxPoolSize="50" 
         minPoolSize="2"
         acquireIncrement="2" 
         jdbcUrl="jdbc:oracle:thin:@192.168.1.229:1521:lead"
         user="lead_oams"
         password="p"/>
            
     <!--使用C3P0配置针对SQLServer数据库的JNDI数据源-->
     <Resource 
         name="jdbc/SqlServerDataSource"
         auth="Container"
         factory="org.apache.naming.factory.BeanFactory" 
         type="com.mchange.v2.c3p0.ComboPooledDataSource"
         driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
         idleConnectionTestPeriod="60"
         maxPoolSize="50" 
         minPoolSize="2"
         acquireIncrement="2" 
         jdbcUrl="jdbc:sqlserver://192.168.1.51:1433;DatabaseName=demo"
         user="sa" 
         password="p@ssw0rd"/>
 </Context>


  2.在web.xml引用JDNI数据源:




 <?xml version="1.0" encoding="UTF-8"?>
 <web-app version="2.5" 
      xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   <!-- 
     res-ref-name:表示引用资源的名称
     res-type:    此资源对应的类型为javax.sql.DataSource
     res-auth:    容器授权管理
   -->
    <!--Oracle数据库JNDI数据源引用 -->
   <resource-ref>
       <description>Oracle DB Connection</description>
       <res-ref-name>jdbc/OracleDataSource</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
  </resource-ref>
   
   <!--MySQL数据库JNDI数据 -->
   <resource-ref>
       <description>MySQL DB Connection</description>
       <res-ref-name>jdbc/MysqlDataSource</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
   </resource-ref>
   
   <!--SQLServer数据库JNDI数据源引用 -->
   <resource-ref>
       <description>SQLServer DB Connection</description>
       <res-ref-name>jdbc/SqlServerDataSource</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
   </resource-ref>
   
 </web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值