C3P0在多線程下的maxPoolSize配置,

來源:互聯網
上載者:User

C3P0在多線程下的maxPoolSize配置,

ETL工具完成的差不多了,今天遇到一個問題,就是給C3P0配置了maxPoolSize為10,目的是想讓整個應用同時獲得的最大的Connection個數為10,但是在測試應用的這一部分之後,發現PostgreSQL端的連結遠遠超過10個。因為工具是多線程的,所以就想,是不是多線程的問題,查了一下Connection的個數,也確實是10*線程個數。於是做了一個測試:

將maxPoolSize配置為5,運行下面的程式:

ComboPooledDataSource cpds = new ComboPooledDataSource("postgres");for (int i = 0; i < 10; i++) {cpds.getConnection();}ComboPooledDataSource cpds2 = new ComboPooledDataSource("postgres");for (int i = 0; i < 10; i++) {cpds2.getConnection();}

maxPoolSize配置為5,一共想要擷取20個連結,這時查看PostgreSQL Server端的串連數為5,正式預期的結果。

在運行下面的程式:

for (int i = 0; i < 2; i++) {new Thread(new Runnable() {@Overridepublic void run() {ComboPooledDataSource cpds = new ComboPooledDataSource("postgres");for (int i = 0; i < 10; i++) {try {cpds.getConnection();} catch (SQLException e) {e.printStackTrace();}}try {Thread.sleep(100000);} catch (InterruptedException e) {e.printStackTrace();}}}, "Thread" + i).start();}

兩個線程,每個線程想要擷取10個連結數,這時查看PostgreSQL Server端的連結數,是10個。將i<2改為i<3,在運行一次,查看PostgreSQL Server端的串連數是15。

再進行下面的測試:

final ComboPooledDataSource cpds = new ComboPooledDataSource("postgres");for (int i = 0; i < 3; i++) {new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 10; i++) {try {cpds.getConnection();} catch (SQLException e) {e.printStackTrace();}}try {Thread.sleep(100000);} catch (InterruptedException e) {e.printStackTrace();}}}, "Thread" + i).start();

建立3個線程,每個線程需要10個Connection,這時查看PostgreSQL Server端的串連數是5,可見,PostgreSQL Server端的連結的個數與用戶端建立的ComboPooledDataSource執行個體的個數成正比,C3P0設定檔中配置的內容都是針對一個ComboPooledDataSource執行個體的限制。

結論:

底層的原因大概是因為C3P0不是為多線程設計的,在底層JDBC也不是安全執行緒的。具體的原因,有機會在具體分析。

一個應用中一般一個資料來源只用一個ComboPooledDataSource對象,也就是一個ComboPooledDataSource執行個體。









聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.