Java 數(shù)據(jù)庫(kù)連接池c3p0 介紹
前言:
c3p0 是一個(gè)開源的數(shù)據(jù)庫(kù)連接池,實(shí)現(xiàn)了 JDBC 3 規(guī)范;本文主要介紹 c3p0 的基本使用,文中使用到的軟件版本:Java 1.8.0_191、c3p0 0.9.5.5、Spring Boot 2.3.12.RELEASE。
1、配置參數(shù)
1.1、基礎(chǔ)配置
配置
參數(shù) | 默認(rèn)值 | 描述 |
driverClass | null | 驅(qū)動(dòng)類名稱 |
jdbcUrl | null | jdbc 連接 url |
user | null | 用戶名 |
password | null | 密碼 |
1.2、連接池大小
參數(shù) | 默認(rèn)值 | 描述 |
acquireIncrement | 3 | 連接池中的連接耗盡時(shí),一次創(chuàng)建的連接個(gè)數(shù) |
initialPoolSize | 3 | 初始連接池大小,介于?minPoolSize 和 maxPoolSize 之間 |
maxPoolSize | 15 | 最大連接數(shù) |
minPoolSize | 3 | 最小連接數(shù) |
1.3、連接池大小和連接存活時(shí)間
參數(shù) | 默認(rèn)值 | 描述 |
maxConnectionAge | 0 | 連接存活的最長(zhǎng)時(shí)間(秒),0 表示沒(méi)有限制。正在使用的連接不會(huì)不受此限制。 |
?maxIdleTime | 0 | ?空閑連接數(shù)最大存活時(shí)間,0 表示永不過(guò)期 |
?maxIdleTimeExcessConnections | 0 | ?當(dāng)連接數(shù)大于?minPoolSize 時(shí),空閑連接數(shù)最大存活時(shí)間,0 表示永不過(guò)期 |
1.4、連接測(cè)試
參數(shù) | 默認(rèn)值 | 描述 |
automaticTestTable | null | 測(cè)試的表名;如果設(shè)置了,c3p0 將使用該表名創(chuàng)建一個(gè)空表,并使用其來(lái)測(cè)試連接,preferredTestQuery 參數(shù)將被忽略。 |
connectionTesterClassName | com.mchange.v2.c3p0.impl.DefaultConnectionTester | 連接測(cè)試的類名,需實(shí)現(xiàn)??com.mchange.v2.c3p0.ConnectionTester 或 com.mchange.v2.c3p0.QueryConnectionTester 接口。 |
idleConnectionTestPeriod | 0 | 空閑連接測(cè)試的間隔(秒)。 |
preferredTestQuery | null | 連接測(cè)試的語(yǔ)句;如果不設(shè)置,將使用?DatabaseMetaData 的?getTables 方法來(lái)測(cè)試,這可能時(shí)比較慢的。 |
testConnectionOnCheckin | false | 連接返回連接池時(shí),是否測(cè)試 |
testConnectionOnCheckout | false | 從連接池獲取連接時(shí),是否測(cè)試 |
1.5、預(yù)編譯池
參數(shù) | 默認(rèn)值 | 描述 |
maxStatements | 0 | 緩存總體預(yù)編譯語(yǔ)句的最大數(shù)量 |
maxStatementsPerConnection | 0 | 緩存每個(gè)連接中預(yù)編譯語(yǔ)句的最大數(shù)量 |
statementCacheNumDeferredCloseThreads | 0 | 清理 statement 緩存的線程數(shù),如果需要設(shè)置,應(yīng)設(shè)置為 1。一些數(shù)據(jù)庫(kù),特別是 oracle 會(huì)在連接使用時(shí)關(guān)閉 statement,數(shù)據(jù)庫(kù)無(wú)法很好的處理這種情況,進(jìn)而導(dǎo)致死鎖。清理的線程會(huì)在連接 |
maxStatements
和?maxStatementsPerConnection
如果都為 0,將不緩存預(yù)編譯語(yǔ)句。如果?maxStatements=0 and?maxStatementsPerConnection>0
,maxStatementsPerConnection
起作用,不限制總的緩存數(shù)量;如果?maxStatements>0 and?maxStatementsPerConnection=0
,maxStatements
起作用,不限單個(gè)連接的緩存數(shù)量。
1.6、數(shù)據(jù)庫(kù)中斷的恢復(fù)
參數(shù) | 默認(rèn)值 | 描述 |
acquireRetryAttempts | 30 | 獲取連接失敗時(shí)的重試次數(shù) |
acquireRetryDelay | 1000 | 連接獲取重試的時(shí)間間隔(毫秒) |
?breakAfterAcquireFailure | ?false | 嘗試獲取連接失敗時(shí),是否聲明連接池?cái)嚅_并永久關(guān)閉 |
1.7、自定義連接生命周期管理
參數(shù) | 默認(rèn)值 | 描述 |
connectionCustomizerClassName | null | 連接生命周期管理的自定義類,需實(shí)現(xiàn)?com.mchange.v2.c3p0.ConnectionCustomizer 接口 |
1.8、處理未提交的事務(wù)
參數(shù) | 默認(rèn)值 | 描述 |
autoCommitOnClose | false | 連接在返回連接池時(shí)是否自動(dòng)提交事務(wù)。true,提交事務(wù);false,回滾事務(wù) |
forceIgnoreUnresolvedTransactions | false | 連接在返回連接池時(shí),是否強(qiáng)制不處理事務(wù);強(qiáng)烈不推薦設(shè)置為 true。 |
1.9、調(diào)試
參數(shù) | 默認(rèn)值 | 描述 |
debugUnreturnedConnectionStackTraces | false | 是否記錄活動(dòng)連接的堆棧信息;如果設(shè)為 true,且?unreturnedConnectionTimeout>0,當(dāng)連接借出時(shí)間 >?unreturnedConnectionTimeout 時(shí),就會(huì)打印連接的堆棧信息,并刪除該連接。 |
unreturnedConnectionTimeout | 0 | 連接未返回連接池的超時(shí)時(shí)間(秒) |
這兩個(gè)參數(shù)可用于幫助發(fā)現(xiàn)連接泄露。
1.10、避免熱部署內(nèi)存泄露
參數(shù) | 默認(rèn)值 | 描述 |
contextClassLoaderSource | caller | 用于生成 c3p0 線程的類加載器來(lái)源,為 caller, library 或 none。caller 表示來(lái)源于連接池的調(diào)用者;library 表示來(lái)源于 c3p0 本身;none 表示使用系統(tǒng)類加載器 |
privilegeSpawnedThreads | false | ?生成 c3p0 線程時(shí)是否使用 c3p0 庫(kù)中的 AccessControlContext;默認(rèn)(false)使用連接池調(diào)用者的 AccessControlContext。 |
在應(yīng)用熱部署、取消部署時(shí),連接池可能會(huì)阻止垃圾回收進(jìn)而導(dǎo)致內(nèi)存泄露;?這兩個(gè)參數(shù)主要用于處理這種情況。
1.11、其它配置
參數(shù) | 默認(rèn)值 | 描述 |
checkoutTimeout | 0 | 從連接獲取連接的超時(shí)時(shí)間,0 表示永不超時(shí) |
factoryClassLocation | null | c3p0 libraries的路徑,如果在本地(通常都是這樣),那么無(wú)需設(shè)置 |
forceSynchronousCheckins | false | 連接返回連接池是否同步方式 |
?maxAdministrativeTaskTime | ?0 | ?管理任務(wù)運(yùn)行的最大時(shí)間(秒),超過(guò)改時(shí)間會(huì)終端任務(wù);0 表示管理任務(wù)永遠(yuǎn)不被打斷。 |
?numHelperThreads | ?3 | ?管理線程個(gè)數(shù) |
usesTraditionalReflectiveProxies | false | 已過(guò)期。是否使用發(fā)射動(dòng)態(tài)代理的方式來(lái)實(shí)現(xiàn) Connection 及 其他 JDBC 接口。 |
詳細(xì)說(shuō)明可參考官網(wǎng)文檔:https://www.mchange.com/projects/c3p0/#configuration
2、原始連接操作
c3p0 提供 api 來(lái)訪問(wèn)原始連接中的非標(biāo)準(zhǔn)接口:
- 把連接轉(zhuǎn)成
C3P0ProxyConnection
- 然后調(diào)用
rawConnectionOperation
方法
下面是獲取 PostgreSQL? JDBC
驅(qū)動(dòng)中 CopyManager
對(duì)象的方法:
Connection connection = (C3P0ProxyConnection) c3p0DataSource.getConnection();
C3P0ProxyConnection castConnection = (C3P0ProxyConnection) connection;
Method method = BaseConnection.class.getMethod("getCopyAPI", new Class[]{});
CopyManager copyManager = (CopyManager) castConnection.rawConnectionOperation(method, C3P0ProxyConnection.RAW_CONNECTION, new Object[]{});
3、使用
3.1、直接使用
3.1.1、引入依賴
<dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency>
3.1.2、使用例子
package com.abc.demo.general.dbpool; import com.mchange.v2.c3p0.ComboPooledDataSource; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class C3p0Case { public static void main(String[] args) throws PropertyVetoException { ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://10.40.9.11:3306/mydb?useUnicode=true&characterEncoding=UTF-8"); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("123456"); comboPooledDataSource.setInitialPoolSize(2); comboPooledDataSource.setMinPoolSize(2); comboPooledDataSource.setMaxPoolSize(10); comboPooledDataSource.setPreferredTestQuery("select 1"); comboPooledDataSource.setIdleConnectionTestPeriod(60); comboPooledDataSource.setTestConnectionOnCheckout(true); comboPooledDataSource.setCheckoutTimeout(1000 * 30); Connection connection = null; Statement st = null; ResultSet rs = null; try { connection = comboPooledDataSource.getConnection(); st = connection.createStatement(); rs = st.executeQuery("select version()"); if (rs.next()) { System.out.println(rs.getString(1)); } } catch (SQLException e) { e.printStackTrace(); } finally { close(connection); } //實(shí)際使用中一般是在應(yīng)用啟動(dòng)時(shí)初始化數(shù)據(jù)源,應(yīng)用從數(shù)據(jù)源中獲取連接;并不會(huì)關(guān)閉數(shù)據(jù)源。 comboPooledDataSource.close(); } private static void close(Connection connection) { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
3.2、在 SpringBoot 中使用
3.1.1、引入依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
3.1.2、單數(shù)據(jù)源
application.yml 配置:
spring: datasource: c3p0: driver-class: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://10.40.9.11:3306/myDb?useUnicode=true&characterEncoding=UTF-8 user: root password: 123456 initial-pool-size: 2 min-pool-size: 2 max-pool-size: 10 preferred-test-query: select 1 idle-connection-test-period: 60 test-connection-on-checkout: true checkout-timeout: 30000
數(shù)據(jù)源配置類:
package com.abc.demo.config; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class DataSourceConfig { @Bean("dataSource") @ConfigurationProperties(prefix = "spring.datasource.c3p0") public DataSource dataSource1() { return DataSourceBuilder.create().type(ComboPooledDataSource.class).build(); } }
使用:
@Autowired private DataSource dataSource;
3.1.3、多數(shù)據(jù)源
application.yml 配置:
spring: datasource: c3p0: db1: driver-class: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://10.40.9.11:3306/myDb?useUnicode=true&characterEncoding=UTF-8 user: root password: InsYR0ot187! initial-pool-size: 2 min-pool-size: 2 max-pool-size: 10 preferred-test-query: select 1 idle-connection-test-period: 60 test-connection-on-checkout: true checkout-timeout: 30000 db2: driver-class: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://10.40.9.12:3306/myDb?useUnicode=true&characterEncoding=UTF-8 user: root password: InsYR0ot187! initial-pool-size: 2 min-pool-size: 2 max-pool-size: 10 preferred-test-query: select 1 idle-connection-test-period: 60 test-connection-on-checkout: true checkout-timeout: 30000
數(shù)據(jù)源配置類:
package com.abc.demo.config; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class DataSourceConfig { @Bean("dataSource1") @ConfigurationProperties(prefix = "spring.datasource.c3p0.db1") public DataSource dataSource1() { return DataSourceBuilder.create().type(ComboPooledDataSource.class).build(); } @Bean("dataSource2") @ConfigurationProperties(prefix = "spring.datasource.c3p0.db2") public DataSource dataSource2() { return DataSourceBuilder.create().type(ComboPooledDataSource.class).build(); } }
使用:
@Autowired @Qualifier("dataSource1") private DataSource dataSource1; @Autowired @Qualifier("dataSource2") private DataSource dataSource2;
到此這篇關(guān)于 Java 數(shù)據(jù)庫(kù)連接池c3p0 介紹的文章就介紹到這了,更多相關(guān) Java 連接池c3p0 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot?實(shí)現(xiàn)不同context-path下的會(huì)話共享
這篇文章主要介紹了springboot?實(shí)現(xiàn)不同context-path下的會(huì)話共享,基于很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01關(guān)于IDEA報(bào)錯(cuò)Error:java 不支持發(fā)行版本17的原因及解決方案
在rebuild或運(yùn)行項(xiàng)目時(shí)提示“Error:java: 錯(cuò)誤: 不支持發(fā)行版本 17”,本文將給大家介紹了IDEA提示“Error:java: 錯(cuò)誤: 不支持發(fā)行版本17”的原因及解決方案,需要的朋友可以參考下2023-09-09Spring中的Schedule動(dòng)態(tài)添加修改定時(shí)任務(wù)詳解
這篇文章主要介紹了Spring中的Schedule動(dòng)態(tài)添加修改定時(shí)任務(wù)詳解,可能有人會(huì)問(wèn),為啥不用Quartz,Quartz自然是非常方便強(qiáng)大的,但不是本篇要講的內(nèi)容,本篇就偏要使用SpringSchedule來(lái)實(shí)現(xiàn)動(dòng)態(tài)的cron表達(dá)式任務(wù),需要的朋友可以參考下2023-11-11基于Spring@Autowired注解與自動(dòng)裝配詳談
下面小編就為大家?guī)?lái)一篇基于Spring@Autowired注解與自動(dòng)裝配詳談。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Java實(shí)現(xiàn)的斷點(diǎn)續(xù)傳功能的示例代碼
本篇文章主要介紹了Java實(shí)現(xiàn)的斷點(diǎn)續(xù)傳功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02SpringBoot解析指定Yaml配置文件的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了SpringBoot解析指定Yaml配置文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03Java中Lambda表達(dá)式的進(jìn)化之路詳解
本文通過(guò)示例大家給大家介紹了Java中Lambda表達(dá)式的進(jìn)化之路,感興趣的的朋友一起看看吧,希望能夠給你帶來(lái)幫助2021-11-11