dbcp 系列
從零開始手寫 mybatis (三)jdbc pool 如何從零手寫實現資料庫連線池 dbcp?
萬字長文深入淺出資料庫連線池 HikariCP/Commons DBCP/Tomcat/c3p0/druid 對比
Database Connection Pool 資料庫連線池概覽
c3p0 資料池入門使用教程
alibaba druid 入門介紹
資料庫連線池 HikariCP 效能為什麼這麼快?
Apache Tomcat DBCP(Database Connection Pool) 資料庫連線池-01-入門介紹
vibur-dbcp 併發、快速且功能完備的 JDBC 連線池,提供先進的效能監控功能-01-入門介紹
c3p0 是什麼?
c3p0是一個易於使用的庫,透過使用jdbc3規範和jdbc2的可選擴充套件定義的功能來擴充套件傳統JDBC驅動程式,從而使其“企業就緒”。
從0.9.5版開始,c3p0完全支援jdbc4規範。
特別是c3p0提供了一些有用的服務:
一個類,它使傳統的基於DriverManager的JDBC驅動程式適應最新的javax.sql.DataSource方案,以獲取資料庫連線。
DataSources後面的Connection和PreparedStatement的透明池可以“包裝”傳統驅動程式或任意非池化DataSources。
該庫盡力使細節正確:
c3p0資料來源既可引用也可序列化,因此適合繫結到各種基於JNDI的命名服務。
檢入池中的Connections和Statements時,會仔細清理Statement和ResultSet,以防止客戶端使用僅清理其Connections的惰性但常見的資源管理策略時資源耗盡。
該庫採用JDBC 2和3規範定義的方法(即使這些與庫作者的首選項衝突)。
資料來源以JavaBean樣式編寫,提供了所有必需和大多數可選屬性(以及一些非標準屬性)以及無引數建構函式。
實現了所有JDBC定義的內部介面(ConnectionPoolDataSource,PooledConnection,生成ConnectionEvent的Connection等)。
您可以將c3p0類與相容的第三方實現混合使用(儘管並非所有c3p0功能都可以與ConnectionPoolDataSource的外部實現一起使用)。
c3p0希望提供的資料來源實現不適合大批次“ J2EE企業應用程式”使用。請提供反饋,錯誤修復等!
入門例子
maven 匯入
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
入門程式碼
透過程式碼顯式指定配置:
ComboPooledDataSource source = new ComboPooledDataSource();
source.setDriverClass("com.mysql.jdbc.Driver");
source.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8");
source.setUser("root");
source.setPassword("123456");
//獲取連結
Connection connection = source.getConnection();
System.out.println(connection.getCatalog());
- 日誌輸出
七月 17, 2020 4:58:21 下午 com.mchange.v2.log.MLog
資訊: MLog clients using java 1.4+ standard logging.
七月 17, 2020 4:58:22 下午 com.mchange.v2.c3p0.C3P0Registry
資訊: Initializing c3p0-0.9.5.5 [built 11-December-2019 22:18:33 -0800; debug? true; trace: 10]
七月 17, 2020 4:58:22 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
資訊: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1bqqx35abpix6b312lrdzj|7bfcd12c, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1bqqx35abpix6b312lrdzj|7bfcd12c, idleConnectionTestPeriod -> 0, initialPoolSize -> 2, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 30, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 2, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
test
透過配置檔案
配置
- c3p0-config.xml
將該配置檔案,置於 resources 資料夾下。
<c3p0-config>
<!-- 預設配置,如果沒有指定則使用這個配置 -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">
<![CDATA[jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8]]>
</property>
<property name="user">root</property>
<property name="password">123456</property>
<!-- 初始化池大小 -->
<property name="initialPoolSize">2</property>
<!-- 最大空閒時間 -->
<property name="maxIdleTime">30</property>
<!-- 最多有多少個連線 -->
<property name="maxPoolSize">10</property>
<!-- 最少幾個連線 -->
<property name="minPoolSize">2</property>
<!-- 每次最多可以執行多少個批處理語句 -->
<property name="maxStatements">50</property>
</default-config>
</c3p0-config>
實現
ComboPooledDataSource source = new ComboPooledDataSource();
Connection connection = source.getConnection();
System.out.println(connection.getCatalog());
日誌
七月 17, 2020 5:00:41 下午 com.mchange.v2.log.MLog
資訊: MLog clients using java 1.4+ standard logging.
七月 17, 2020 5:00:41 下午 com.mchange.v2.c3p0.C3P0Registry
資訊: Initializing c3p0-0.9.5.5 [built 11-December-2019 22:18:33 -0800; debug? true; trace: 10]
七月 17, 2020 5:00:42 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
資訊: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1bqqx35abpj05mbhotd60|27082746, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1bqqx35abpj05mbhotd60|27082746, idleConnectionTestPeriod -> 0, initialPoolSize -> 2, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 30, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 2, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
test