MENU

SpringBoot整合c3p0

添加依赖

pom.xml

<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.5</version>
</dependency>

添加配置

application.properties

#配置c3p0的数据池
#配置数据库名
c3p0.jdbcUrl=jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8&useSSL=false
#配置username
c3p0.user=root
#配置password
c3p0.password=toor
#配置driver
c3p0.driverClass=com.mysql.jdbc.Driver
#配置最小连接池数据
c3p0.minPoolSize=2
#配置最大连接数
c3p0.maxPoolSize=10
#最大空闲时间
c3p0.maxIdleTime=1800000
#当连接池中的连接耗尽的时候c3p0一次同时获取的连接数,每次增加3个连接
c3p0.acquireIncrement=3
#最大sql语句
c3p0.maxStatements=1000
#初始化连接池数
c3p0.initialPoolSize=3
c3p0.idleConnectionTestPeriod=30
c3p0.acquireRetryAttempts=30
c3p0.acquireRetryDelay=1000
c3p0.breakAfterAcquireFailure=false
c3p0.testConnectionOnCheckout=false

创建配置类

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@SpringBootConfiguration
public class DataSourceConfig {
    //指定当前对象作为bean
    @Bean(name = "dataSource")
    //指定dataSource来DI
    @Qualifier(value = "dataSource")
    //primary将当前数据库连接池作为默认数据库连接池
    @Primary
    //在application.properties文件中增加前缀c3p0
    @ConfigurationProperties(prefix = "c3p0")
    public DataSource dataSource(){
        return DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build();
    }

}

至此 springboot整合c3p0完成


文章标题:SpringBoot整合c3p0
如果文中内容侵犯了您的权益,请及时与博主取得联系进行删除!
本站文章未经许可禁止转载,本文地址:https://blog.wanvale.com/archives/128/

Last Modified: June 20, 2020
Archives QR Code Tip
QR Code for this page
Tipping QR Code