“C3P0数据库连接池的两种实现方式”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第23行: | 第23行: | ||
==== 相关配置属性 ==== | ==== 相关配置属性 ==== | ||
https://www.mchange.com/projects/c3p0/#configuration_properties | https://www.mchange.com/projects/c3p0/#configuration_properties | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === 测试获取连接 === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.connection; | ||
+ | |||
+ | import com.mchange.v2.c3p0.ComboPooledDataSource; | ||
+ | import org.junit.Test; | ||
+ | |||
+ | import java.beans.PropertyVetoException; | ||
+ | import java.sql.Connection; | ||
+ | import java.sql.SQLException; | ||
+ | |||
+ | public class C3P0Test { | ||
+ | |||
+ | @Test | ||
+ | public void testGetConnection() throws PropertyVetoException, SQLException { | ||
+ | |||
+ | //获取 C3P0 数据库连接池 | ||
+ | ComboPooledDataSource cpds = new ComboPooledDataSource(); | ||
+ | cpds.setDriverClass( "com.mysql.cj.jdbc.Driver" ); //loads the jdbc driver | ||
+ | cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/jdbc_test?rewriteBatchedStatements=true" ); | ||
+ | cpds.setUser("root"); | ||
+ | cpds.setPassword("123456"); | ||
+ | |||
+ | //通过设置相关的参数,对数据库连接池进行管理: | ||
+ | //设置初始时数据库连接池中的连接数 | ||
+ | cpds.setInitialPoolSize(10); | ||
+ | |||
+ | Connection conn = cpds.getConnection(); | ||
+ | System.out.println(conn); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | I:\Java\jdk1.8.0_101\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\lib\idea_rt.jar=31740:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\plugins\junit\lib\junit5-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\plugins\junit\lib\junit-rt.jar;I:\Java\jdk1.8.0_101\jre\lib\charsets.jar;I:\Java\jdk1.8.0_101\jre\lib\deploy.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\dnsns.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\jaccess.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\localedata.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\nashorn.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\sunec.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\zipfs.jar;I:\Java\jdk1.8.0_101\jre\lib\javaws.jar;I:\Java\jdk1.8.0_101\jre\lib\jce.jar;I:\Java\jdk1.8.0_101\jre\lib\jfr.jar;I:\Java\jdk1.8.0_101\jre\lib\jfxswt.jar;I:\Java\jdk1.8.0_101\jre\lib\jsse.jar;I:\Java\jdk1.8.0_101\jre\lib\management-agent.jar;I:\Java\jdk1.8.0_101\jre\lib\plugin.jar;I:\Java\jdk1.8.0_101\jre\lib\resources.jar;I:\Java\jdk1.8.0_101\jre\lib\rt.jar;E:\record\2022\12\21\jdbc_2\target\test-classes;E:\record\2022\12\21\jdbc_2\target\classes;I:\maven_repository\junit\junit\4.13\junit-4.13.jar;I:\maven_repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;I:\maven_repository\mysql\mysql-connector-java\8.0.30\mysql-connector-java-8.0.30.jar;I:\maven_repository\com\google\protobuf\protobuf-java\3.19.4\protobuf-java-3.19.4.jar;I:\maven_repository\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 io.github.jihch.connection.C3P0Test,testGetConnection | ||
+ | 一月 10, 2023 5:25:24 下午 com.mchange.v2.log.MLog <clinit> | ||
+ | 信息: MLog clients using java 1.4+ standard logging. | ||
+ | 一月 10, 2023 5:25:24 下午 com.mchange.v2.c3p0.C3P0Registry banner | ||
+ | 信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10] | ||
+ | 一月 10, 2023 5:25:24 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager | ||
+ | 信息: 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, dataSourceName -> 1hgeby9at1708kzd10rvblt|43556938, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.cj.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgeby9at1708kzd10rvblt|43556938, idleConnectionTestPeriod -> 0, initialPoolSize -> 10, jdbcUrl -> jdbc:mysql://localhost:3306/jdbc_test?rewriteBatchedStatements=true, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ] | ||
+ | com.mchange.v2.c3p0.impl.NewProxyConnection@668bc3d5 | ||
+ | |||
+ | Process finished with exit code 0 | ||
+ | |||
+ | </syntaxhighlight> |
2023年1月10日 (二) 09:27的版本
https://www.bilibili.com/video/BV1eJ411c7rf?p=47
在 pom.xml 中加入依赖
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
参考C3P0官方文档
快速开始
https://www.mchange.com/projects/c3p0/#quickstart
相关配置属性
https://www.mchange.com/projects/c3p0/#configuration_properties
测试获取连接
package io.github.jihch.connection;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
public class C3P0Test {
@Test
public void testGetConnection() throws PropertyVetoException, SQLException {
//获取 C3P0 数据库连接池
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "com.mysql.cj.jdbc.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/jdbc_test?rewriteBatchedStatements=true" );
cpds.setUser("root");
cpds.setPassword("123456");
//通过设置相关的参数,对数据库连接池进行管理:
//设置初始时数据库连接池中的连接数
cpds.setInitialPoolSize(10);
Connection conn = cpds.getConnection();
System.out.println(conn);
}
}
I:\Java\jdk1.8.0_101\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\lib\idea_rt.jar=31740:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\plugins\junit\lib\junit5-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\plugins\junit\lib\junit-rt.jar;I:\Java\jdk1.8.0_101\jre\lib\charsets.jar;I:\Java\jdk1.8.0_101\jre\lib\deploy.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\dnsns.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\jaccess.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\localedata.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\nashorn.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\sunec.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;I:\Java\jdk1.8.0_101\jre\lib\ext\zipfs.jar;I:\Java\jdk1.8.0_101\jre\lib\javaws.jar;I:\Java\jdk1.8.0_101\jre\lib\jce.jar;I:\Java\jdk1.8.0_101\jre\lib\jfr.jar;I:\Java\jdk1.8.0_101\jre\lib\jfxswt.jar;I:\Java\jdk1.8.0_101\jre\lib\jsse.jar;I:\Java\jdk1.8.0_101\jre\lib\management-agent.jar;I:\Java\jdk1.8.0_101\jre\lib\plugin.jar;I:\Java\jdk1.8.0_101\jre\lib\resources.jar;I:\Java\jdk1.8.0_101\jre\lib\rt.jar;E:\record\2022\12\21\jdbc_2\target\test-classes;E:\record\2022\12\21\jdbc_2\target\classes;I:\maven_repository\junit\junit\4.13\junit-4.13.jar;I:\maven_repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;I:\maven_repository\mysql\mysql-connector-java\8.0.30\mysql-connector-java-8.0.30.jar;I:\maven_repository\com\google\protobuf\protobuf-java\3.19.4\protobuf-java-3.19.4.jar;I:\maven_repository\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 io.github.jihch.connection.C3P0Test,testGetConnection
一月 10, 2023 5:25:24 下午 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
一月 10, 2023 5:25:24 下午 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
一月 10, 2023 5:25:24 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: 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, dataSourceName -> 1hgeby9at1708kzd10rvblt|43556938, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.cj.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgeby9at1708kzd10rvblt|43556938, idleConnectionTestPeriod -> 0, initialPoolSize -> 10, jdbcUrl -> jdbc:mysql://localhost:3306/jdbc_test?rewriteBatchedStatements=true, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
com.mchange.v2.c3p0.impl.NewProxyConnection@668bc3d5
Process finished with exit code 0