C3P0連接池的配置與使用

1、下載c3p0-0.9.1.2.jar

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


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



3、配置文件內容如下:

[html] view plain copy
  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、連接池連接類

[java] view plain copy
  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、測試

[java] view plain copy
  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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章