第14章—数据库连接池(C3P0)
spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html
码云源码地址:https://gitee.com/jinxiaohang/springboot
C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate,Spring等。
本次练习在之前整合Spring Data JPA基础上进行。
一、引入依赖
<dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
二、添加配置文件
c3p0:
driverClass: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/test
user: root
password: root
maxStatements: 1000
breakAfterAcquireFailure: false
maxIdleTime: 1800000
acquireRetryAttempts: 30
testConnectionOnCheckout: false
minPoolSize: 2
acquireIncrement: 3
acquireRetryDelay: 1000
idleConnectionTestPeriod: 60
initialPoolSize: 3
maxPoolSize: 10
spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
三、添加配置类
@Configuration public class DataSourceConfig { @Bean(name = "dataSource") @Qualifier(value = "dataSource") @Primary @ConfigurationProperties(prefix = "c3p0") public DataSource dataSource() { return DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build(); } }
四、启动测试
正常启动,完成C3P0数据库连接池整合。