溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

c3p0數(shù)據(jù)庫連接池如何使用

發(fā)布時(shí)間:2021-08-03 16:54:35 來源:億速云 閱讀:119 作者:Leah 欄目:數(shù)據(jù)庫

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)c3p0數(shù)據(jù)庫連接池如何使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

一、jar包

引用開源項(xiàng)目,自然要先下載人家的jar包,我這里有一個(gè)在云盤:c3p0-0.9.5-pre4.jar,這里面其實(shí)有三個(gè)包,是我在開源中國下載的最新的,如果你還想要更新的不妨自己到網(wǎng)上去搜一下。

二、配置文件

作為一個(gè)數(shù)據(jù)庫連接池自然有很多參數(shù)要設(shè)置,當(dāng)然就算你不設(shè)置也有默認(rèn)的,不過那不一定能滿足你的要求。這里的配置文件沒有什么特別的要求,可以是xml也可以是properties甚至與txt都行,當(dāng)然如果你愿意也可以寫死在程序中。

下面是我的配置文件,我把它放在了src目錄下,名字叫“c3p0.properties”

[java] view
plaincopy

  1. #jdbc基本信息  

  2. driverClass=oracle.jdbc.driver.OracleDriver  

  3. jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521/orcl  

  4. user=bctts  

  5. password=bctts123  


  6. #c3p0連接池信息  

  7. c3p0.minPoolSize=3

  8. c3p0.maxPoolSize=25


  9. #當(dāng)連接池中的連接耗盡的時(shí)候c3p0一次同時(shí)獲取的連接數(shù)  

  10. c3p0.acquireIncrement=3

  11. #定義在從數(shù)據(jù)庫獲取新連接失敗后重復(fù)嘗試的次數(shù)  

  12. c3p0.acquireRetryAttempts=60

  13. #兩次連接中間隔時(shí)間,單位毫秒  

  14. c3p0.acquireRetryDelay=1000

  15. #連接關(guān)閉時(shí)默認(rèn)將所有未提交的操作回滾  

  16. c3p0.autoCommitOnClose=false

  17. #當(dāng)連接池用完時(shí)客戶端調(diào)用getConnection()后等待獲取新連接的時(shí)間,超時(shí)后將拋出SQLException,如設(shè)為0則無限期等待。單位毫秒  

  18. c3p0.checkoutTimeout=3000

  19. #每120秒檢查所有連接池中的空閑連接。Default: 0

  20. c3p0.idleConnectionTestPeriod=120

  21. #最大空閑時(shí)間,60秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄。Default: 0

  22. c3p0.maxIdleTime=600

  23. #如果設(shè)為true那么在取得連接的同時(shí)將校驗(yàn)連接的有效性。Default: false

  24. c3p0.testConnectionOnCheckin=true

  25. #c3p0將建一張名為c3p0TestTable的空表,并使用其自帶的查詢語句進(jìn)行測試。  

  26. jdbc.automaticTestTable = c3p0TestTable  


配置文件中的內(nèi)容我就不解釋了,相信你看一眼就能很明白。

三、代碼實(shí)現(xiàn)

c3p0的使用非常簡單,基本上就是三步搞定。

DataSource unPooled = DataSources.unpooledDataSource(url,jdbcproperties);//這里的第二個(gè)參數(shù)放的是連接數(shù)據(jù)庫的信息,包括user、password等信息

DataSource ds = DataSources.pooledDataSource(unPooled,c3propertis);//這里面的第二個(gè)參數(shù)放的是c3p0連接池的配置信息

Connection conn = ds.getConnection();

這三句代碼相信大家很容易看明白,也是使用c3p0連接池的核心代碼,真的很簡單。

下面是我寫的例子,僅供參考

[java] view
plaincopy

  1. package com.bks.db;  


  2. import java.io.FileInputStream;  

  3. import java.sql.Connection;  

  4. import java.sql.SQLException;  

  5. import java.util.Properties;  


  6. import javax.sql.DataSource;  


  7. import com.mchange.v2.c3p0.DataSources;  


  8. /**

  9.  * c3p0連接池管理類

  10.  * @author ICE

  11.  *

  12.  */

  13. public class C3P0ConnentionProvider {  


  14.     private static final String JDBC_DRIVER = "driverClass";  

  15.     private static final String JDBC_URL = "jdbcUrl";  


  16.     private static DataSource ds;  

  17.     /**

  18.      * 初始化連接池代碼塊

  19.      */

  20.     static{  

  21.         initDBSource();  

  22.     }  


  23.     /**

  24.      * 初始化c3p0連接池

  25.      */

  26.     private static final void initDBSource(){  

  27.         Properties c3p0Pro = new Properties();  

  28.         try {  

  29.             //加載配置文件

  30.             c3p0Pro.load(new FileInputStream("./bin/c3p0.properties"));  

  31.         } catch (Exception e) {  

  32.             e.printStackTrace();  

  33.         }   


  34.         String drverClass = c3p0Pro.getProperty(JDBC_DRIVER);  

  35.         if(drverClass != null){  

  36.             try {  

  37.                 //加載驅(qū)動(dòng)類

  38.                 Class.forName(drverClass);  

  39.             } catch (ClassNotFoundException e) {  

  40.                 e.printStackTrace();  

  41.             }  


  42.         }  


  43.         Properties jdbcpropes = new Properties();  

  44.         Properties c3propes = new Properties();  

  45.         for(Object key:c3p0Pro.keySet()){  

  46.             String skey = (String)key;  

  47.             if(skey.startsWith("c3p0.")){  

  48.                 c3propes.put(skey, c3p0Pro.getProperty(skey));  

  49.             }else{  

  50.                 jdbcpropes.put(skey, c3p0Pro.getProperty(skey));  

  51.             }  

  52.         }  


  53.         try {  

  54.             //建立連接池

  55.             DataSource unPooled = DataSources.unpooledDataSource(c3p0Pro.getProperty(JDBC_URL),jdbcpropes);  

  56.             ds = DataSources.pooledDataSource(unPooled,c3propes);  


  57.         } catch (SQLException e) {  

  58.             e.printStackTrace();  

  59.         }  

  60.     }  


  61.     /**

  62.      * 獲取數(shù)據(jù)庫連接對(duì)象

  63.      * @return 數(shù)據(jù)連接對(duì)象

  64.      * @throws SQLException 

  65.      */

  66.     public static synchronized Connection getConnection() throws SQLException{  

  67.         final Connection conn = ds.getConnection();  

  68.         conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);  

  69.         return conn;  

  70.     }  

  71. }  


下面這個(gè)是測試的例子

[java] view
plaincopy

  1. package test.demo;  


  2. import java.sql.Connection;  

  3. import java.sql.PreparedStatement;  

  4. import java.sql.ResultSet;  

  5. import java.sql.SQLException;  


  6. import com.bks.db.C3P0ConnentionProvider;  


  7. public class Demo {  


  8.     public static void main(String[] args) {  

  9.         for(int i=0;i<30;i++){  

  10.             new Thread(new Demo2()).start();  

  11.         }  

  12.     }  


  13. }  


  14. class Demo2 implements Runnable{  


  15.     public void run(){  

  16.         try {  

  17.             Connection conn = C3P0ConnentionProvider.getConnection();  

  18.             PreparedStatement pst = conn.prepareStatement("select to_char(sysdate,'hh34:mm:ss') tim from dual");  

  19.             ResultSet rs = pst.executeQuery();  

  20.             rs.next();  

  21.             System.out.println(Thread.currentThread().getName()+":"+rs.getString(1));  

  22.             try {  

  23.                 Thread.sleep(10*1000);  

  24.             } catch (InterruptedException e) {  

  25.                 e.printStackTrace();  

  26.             }  


  27.         } catch (SQLException e) {  

  28.             e.printStackTrace();  

  29.         }  

  30.     }  

  31. }  

上述就是小編為大家分享的c3p0數(shù)據(jù)庫連接池如何使用了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI