mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加连接池示例
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
# 数据访问
|
||||
|
||||
* [《芋道 Spring Boot Redis 入门》](http://www.iocoder.cn/Spring-Boot/Redis/?github) 对应 [lab-11](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-11) 。
|
||||
* [《芋道 Spring Boot 数据库连接池入门》](http://www.iocoder.cn/Spring-Boot/datasource-pool/?github) 对应 [lab-19](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-19) 。
|
||||
* [《芋道 Spring Boot MyBatis 入门》](http://www.iocoder.cn/Spring-Boot/MyBatis/?github) 对应 [lab-12](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-12) 。
|
||||
* [《芋道 Spring Boot JPA 入门》](http://www.iocoder.cn/Spring-Boot/JPA/?github) 对应 [lab-13](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-13) 。
|
||||
* [《芋道 Spring Boot JdbcTemplate 入门》](http://www.iocoder.cn/Spring-Boot/JdbcTemplate/?github) 对应 [lab-14](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-14) 。
|
||||
|
||||
@@ -8,8 +8,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application implements CommandLineRunner {
|
||||
@@ -30,20 +28,10 @@ public class Application implements CommandLineRunner {
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
// orders 数据源
|
||||
try (Connection conn = ordersDataSource.getConnection()) {
|
||||
// 这里,可以做点什么
|
||||
logger.info("[run][ordersDataSource 获得连接:{}]", conn);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
logger.info("[run][获得数据源:{}]", ordersDataSource.getClass());
|
||||
|
||||
// users 数据源
|
||||
try (Connection conn = usersDataSource.getConnection()) {
|
||||
// 这里,可以做点什么
|
||||
logger.info("[run][usersDataSource 获得连接:{}]", conn);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
logger.info("[run][获得数据源:{}]", usersDataSource.getClass());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ spring:
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
username: root
|
||||
password:
|
||||
type: com.alibaba.druid.pool.DruidDataSource # 设置类型为 DruidDataSource
|
||||
# Druid 自定义配置,对应 DruidDataSource 中的 setting 方法的属性
|
||||
min-idle: 0 # 池中维护的最小空闲连接数,默认为 0 个。
|
||||
max-active: 20 # 池中最大连接数,包括闲置和使用中的连接,默认为 8 个。
|
||||
@@ -16,6 +17,7 @@ spring:
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
username: root
|
||||
password:
|
||||
type: com.alibaba.druid.pool.DruidDataSource # 设置类型为 DruidDataSource
|
||||
# Druid 自定义配置,对应 DruidDataSource 中的 setting 方法的属性
|
||||
min-idle: 0 # 池中维护的最小空闲连接数,默认为 0 个。
|
||||
max-active: 20 # 池中最大连接数,包括闲置和使用中的连接,默认为 8 个。
|
||||
|
||||
Reference in New Issue
Block a user