mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
perf: 提前创建数据库连接池
Spring Boot 新版本默认会在第一次请求数据库时创建连接池
This commit is contained in:
parent
60488258f5
commit
9d8709ed2d
@ -1,5 +1,6 @@
|
||||
package io.github.xxyopen.novel;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
@ -15,6 +16,7 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.util.Map;
|
||||
|
||||
@SpringBootApplication
|
||||
@ -39,7 +41,14 @@ public class NovelApplication {
|
||||
});
|
||||
// 提前创建连接池,而不是在第一次访问数据库时才创建,提高第一次访问接口的速度
|
||||
log.info("创建连接池...");
|
||||
dataSource.getConnection();
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
HikariDataSource hikariDataSource = (HikariDataSource) dataSource;
|
||||
log.info("最小空闲连接数:{}", hikariDataSource.getMinimumIdle());
|
||||
log.info("最大连接数:{}", hikariDataSource.getMaximumPoolSize());
|
||||
log.info("创建连接池完成.");
|
||||
log.info("数据库:{}", connection.getMetaData().getDatabaseProductName());
|
||||
log.info("数据库版本:{}", connection.getMetaData().getDatabaseProductVersion());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user