tomcat下配置数据库连接池DBCP、C3P0、Proxool

转自:http://blog.163.com/czy_sysu/blog/static/13069559920100792018859/

1、将连接池的jar文件拷贝到tomcat的lib目录下

(1)dbcp:

tomcat默认包含tomcat-dbcp.jar,如果使用tomcat自带的dbcp则不用拷贝jar文件。

如果要使用commons-dbcp连接池,则要将commons-dbcp-xxx.jar和commons-pool.jar拷贝到tomcat的lib目录下。

(2)c3p0:

拷贝c3p0-xx.jar到tomcat的lib目录下

(3)proxool:

拷贝proxool-xx.jar、proxool-cglib.jar、commons-logging-xxx.jar到tomcat的lib目录下

2、打开tomcat的context.xml,进行如下配置:

<!--dbcp-->
 <Resource 
    name="jdbc/mysqlds-dbcp" 
    auth="Container" 
    type= "javax.sql.DataSource" 
 factory="org.apache.commons.dbcp.BasicDataSourceFactory"<!--这里使用的commons-dbcp连接池-->

<!--tomcat的dbcp对应的factory为:将commons替换为tomcat,也可不用配置-->
    driverClassName="com.mysql.jdbc.Driver" 
    url="jdbc:mysql://localhost:3306/test"
    username="root" 
    password=""
    maxActive="100" 
    maxIdle="30"
    maxWait="10000" >  
 </Resource> 
  <!--c3p0-->
 <Resource auth="Container"
     description="DB Connection"
     driverClass="com.mysql.jdbc.Driver"
     maxPoolSize="100"
     minPoolSize="2"
     acquireIncrement="2"
     name="jdbc/mysqlds-c3p0"
     user="root"
     password=""
     factory="org.apache.naming.factory.BeanFactory"
     type="com.mchange.v2.c3p0.ComboPooledDataSource"
     jdbcUrl="jdbc:mysql://localhost:3306/test" />

<!--proxool-->

<Resource name="jdbc/mysqlds-proxool"  
     auth="Container"       
     type="javax.sql.DataSource"  
     factory="org.logicalcobwebs.proxool.ProxoolDataSource"       
     proxool.alias="test"       
     user="root"     
     password=""       
     delegateProperties="foo=1,bar=true"  
     proxool.jndi-name="mysqljndi"      
     proxool.driver-url="jdbc:mysql://127.0.0.1:3306/test"       
     proxool.driver-class="com.mysql.jdbc.Driver"     
     proxool.house-keeping-sleep-time="900000"     
     proxool.maximum-active-time="5"  
     proxool.prototype-count="3"  
     proxool.statistics="1m,15m,1d"  
     proxool.simultaneous-build-throttle="10"  
     proxool.minimum-connection-count="5"  
     proxool.maximum-connection-count="15"    
  />  

3、在web.xml中配置如下代码:

<resource-ref>   
  <res-ref-name>jdbc/mysqlds-proxool</res-ref-name>  <!--与context.xml下的Resources的name属性一致-->
  <res-type>javax.sql.DataSource</res-type>   
  <res-auth>Container</res-auth>   
</resource-ref>

4、获取连接java代码:

   Context context = new InitialContext();
   ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysqlds-proxool");//与context.xml下的Resources的name属性一致

5、常见问题:

      (1)create a new connection but it fail its test:

       在配置proxool连接池时,可能会包以上错误,可以看看你的context.xml配置,将proxool.test-before-use="true"该行去掉即可,即在使用前不要进行测试。

posted @ 2017-09-10 14:07  gdwkong  阅读(487)  评论(0编辑  收藏  举报