C3P0连接池的配置与使用

1、下载c3p0-0.9.1.2.jar

下载地址:http://download.csdn.net/detail/chunxiaqiudong5/9661922

2、添加配置文件c3p0-config.xml

3、配置文件内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <c3p0-config>    
  3.     <!-- This is default config! -->    
  4.     <default-config>    
  5.         <property name="initialPoolSize">10</property>    
  6.         <property name="maxIdleTime">30</property>    
  7.         <property name="maxPoolSize">100</property>    
  8.         <property name="minPoolSize">10</property>    
  9.         <property name="maxStatements">200</property>    
  10.     </default-config>    
  11.     
  12.     <!-- This is my config for mysql-->    
  13.     <named-config name="mysql">    
  14.         <property name="driverClass">com.mysql.jdbc.Driver</property>    
  15.         <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF8</property>    
  16.         <property name="user">root</property>    
  17.         <property name="password"></property>    
  18.          <!-- 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3-->  
  19.         <property name="initialPoolSize">10</property>  
  20.         <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0 -->    
  21.         <property name="maxIdleTime">30</property>    
  22.         <!--连接池中保留的最大连接数。默认值: 15 -->  
  23.         <property name="maxPoolSize">100</property>   
  24.         <!-- 连接池中保留的最小连接数,默认为:3-->   
  25.         <property name="minPoolSize">10</property>   
  26.         <!--c3p0全局的PreparedStatements缓存的大小。如果maxStatements与maxStatementsPerConnection均为0,则缓存不生效,只要有一个不为0,则语句的缓存就能生效。如果默认值: 0-->   
  27.         <property name="maxStatements">200</property>    
  28.         <!-- 当连接池连接耗尽时,客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。默认: 0 -->     
  29.         <property name="checkoutTimeout" value="3000"/>   
  30.         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 -->     
  31.         <property name="acquireIncrement" value="2"/>   
  32.         <!--定义在从数据库获取新连接失败后重复尝试的次数。默认值: 30 ;小于等于0表示无限次-->     
  33.         <property name="acquireRetryAttempts" value="0"/>    
  34.         <!--重新尝试的时间间隔,默认为:1000毫秒-->     
  35.         <property name="acquireRetryDelay" value="1000" />   
  36.         <!--关闭连接时,是否提交未提交的事务,默认为false,即关闭连接,回滚未提交的事务 -->     
  37.         <property name="autoCommitOnClose">false</property>    
  38.         <!--c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试使用。默认值: null -->     
  39.         <property name="automaticTestTable">Test</property>   
  40.          <!--如果为false,则获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常,但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。默认: false-->     
  41.         <property name="breakAfterAcquireFailure">false</property>  
  42.         <!--每60秒检查所有连接池中的空闲连接。默认值: 0,不检查 -->     
  43.         <property name="idleConnectionTestPeriod">60</property>      
  44.         <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。默认值: 0 -->     
  45.         <property name="maxStatementsPerConnection"></property>   
  46.     </named-config>    
  47.         
  48.         
  49.     <!-- This is my config for oracle -->    
  50.     <named-config name="oracle">    
  51.         <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>    
  52.         <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property>    
  53.         <property name="user">scott</property>    
  54.         <property name="password">liang</property>    
  55.         <property name="initialPoolSize">10</property>    
  56.         <property name="maxIdleTime">30</property>    
  57.         <property name="maxPoolSize">100</property>    
  58.         <property name="minPoolSize">10</property>    
  59.         <property name="maxStatements">200</property>    
  60.     </named-config>    
  61. </c3p0-config>  



4、连接池连接类

扫描二维码关注公众号,回复: 1109577 查看本文章
 
  1. package com.xxx.utils;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.SQLException;  
  5.   
  6. import javax.sql.DataSource;  
  7.   
  8. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  9.   
  10. public class JDBCUtil {  
  11.   
  12.     private static DataSource dataSource=null;  
  13.     static{  
  14.         dataSource=new ComboPooledDataSource("mysql");  
  15.     }  
  16.       
  17.     /** 
  18.      * 获取数据库连接 
  19.      * @return 
  20.      */  
  21.     public static Connection getConnection(){  
  22.         Connection conn=null;  
  23.         try {  
  24.              conn=dataSource.getConnection();  
  25.         } catch (SQLException e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.         return conn;  
  29.     }  
  30.   
  31.       
  32.     /** 
  33.      * 关闭数据库连接 
  34.      * @param conn 
  35.      */  
  36.     public static void closeConn(Connection conn){  
  37.         try {  
  38.             if(conn!=null && conn.isClosed()){  
  39.                 conn.close();  
  40.             }  
  41.         } catch (SQLException e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.     }  
  45. }  



5、测试

 
  1. package com.xxx.test;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7.   
  8. import org.junit.Test;  
  9.   
  10. import com.xxx.utils.JDBCUtil;  
  11.   
  12. public class TestJdbc {  
  13.   
  14.     @Test  
  15.     public void test() {  
  16.         Connection conn=JDBCUtil.getConnection();  
  17.         System.out.println(conn);  
  18.         try {  
  19.             PreparedStatement stmt=conn.prepareStatement("select * from tb_user");  
  20.             ResultSet re=stmt.executeQuery();  
  21.             while(re.next()){  
  22.                 String name=re.getString(2);  
  23.                 System.out.println(name);  
  24.                   
  25.             }  
  26.               
  27.               
  28.         } catch (SQLException e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.       
  34.       
  35.   
  36. }  



猜你喜欢

转载自www.cnblogs.com/zmwf/p/9108736.html