C3P0数据源配置
一:添加依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>${c3p0.version}</version> </dependency>
二:添加配置默认二种方式配置文件,XML。在源码com.mchange.v2.c3p0.cfg包下
2.1:c3p0.properties
driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password= initialPoolSize=10 maxIdleTime=50 maxPoolSize=20 minPoolSize=5
2.2:c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/dev</property> <property name="user">root</property> <property name="password"></property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> </default-config> <named-config name="mySource"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property> <property name="user">root</property> <property name="password"></property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> </named-config> </c3p0-config>
三:配置数据源
package com.jachs.c3p0.config; import java.beans.PropertyVetoException; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import com.mchange.v2.c3p0.ComboPooledDataSource; import com.mchange.v2.c3p0.cfg.C3P0ConfigUtils; /*** * * @author zhanchaohan * */ public class C3p0Utill { /*** * 加载Properties方式初始化数据源 * @return * @throws FileNotFoundException * @throws IOException * @throws PropertyVetoException * @see C3P0ConfigUtils */ public ComboPooledDataSource initProperties() throws FileNotFoundException, IOException, PropertyVetoException { Properties properties=C3P0ConfigUtils.findResourceProperties(); ComboPooledDataSource cpds=new ComboPooledDataSource(); // cpds.setProperties(properties);//不起作用 cpds.setDriverClass(properties.getProperty("driverClassName")); cpds.setJdbcUrl(properties.getProperty("url")); cpds.setUser(properties.getProperty("username")); cpds.setPassword(properties.getProperty("password")); cpds.setInitialPoolSize(Integer.parseInt(properties.getProperty("initialPoolSize"))); cpds.setMaxIdleTime(Integer.parseInt(properties.getProperty("maxIdleTime"))); cpds.setMaxPoolSize(Integer.parseInt(properties.getProperty("maxPoolSize"))); cpds.setMinPoolSize(Integer.parseInt(properties.getProperty("minPoolSize"))); return cpds; } /*** * * @return * @see C3P0ConfigXmlUtils */ public ComboPooledDataSource initXML() { // ComboPooledDataSource cpds=new ComboPooledDataSource("mySource"); ComboPooledDataSource cpds=new ComboPooledDataSource();//不给参数使用默认数据源 return cpds; } }
四:测试
package com.jachs.c3p0.config; import java.beans.PropertyVetoException; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Before; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; /*** * * @author zhanchaohan * */ public class C3P0Test { private C3p0Utill c3p0Utill = new C3p0Utill(); ComboPooledDataSource prCpds; ComboPooledDataSource xmlCpds; @Before public void init() throws FileNotFoundException, IOException, PropertyVetoException { prCpds = c3p0Utill.initProperties(); xmlCpds = c3p0Utill.initXML(); } @Test public void tetcPrCpds() throws SQLException { Connection connection = prCpds.getConnection(); ResultSet resultSet = connection.prepareStatement("show tables").executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getString(1)); } } @Test public void tetcxmlCpds() throws SQLException { Connection connection = xmlCpds.getConnection(); ResultSet resultSet = connection.prepareStatement("show tables").executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getString(1)); } } }
分类:
Java
, DataSource
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)