查看“C3P0数据库连接池的两种实现方式”的源代码
←
C3P0数据库连接池的两种实现方式
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
https://www.bilibili.com/video/BV1eJ411c7rf?p=47 === 在 pom.xml 中加入依赖 === <syntaxhighlight lang="xml"> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> </syntaxhighlight> === 参考C3P0官方文档 === ==== https://www.mchange.com/projects/c3p0/ ==== === 测试获取连接方式一:硬编码配置参数 === 参考 https://www.mchange.com/projects/c3p0/#quickstart<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> ==== C3P0 数据库连接池的相关配置属性 ==== https://www.mchange.com/projects/c3p0/#configuration_properties === 测试获取连接方式二:properties 配置文件 === 参考 https://www.mchange.com/projects/c3p0/#configuration_files
返回至
C3P0数据库连接池的两种实现方式
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息