How to use springboot not to automatically initialize database connection pool
Editor to share with you how to use springboot not automatically initialize database connection pool, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Brief introduction of springboot not initializing database connection pool automatically
Sometimes we want to initialize the database connection pool dynamically, but the @ SpringBootApplication annotation of springboot automatically initializes the database connection pool. If it is not configured, it will fail to start, as shown below.
Exception encountered during context initialization-cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Dbcp2.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.commons.dbcp2.BasicDataSource]: Factory method' dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
INFO-Unregistering JMX-exposed beans on shutdown
Solution
The way to do this is to exclude classes that are automatically initialized
@ SpringBootApplication (exclude = {DataSourceAutoConfiguration.class}) public class Application implements CommandLineRunner {...}
Add this sentence
(exclude = {DataSourceAutoConfiguration.class})
You can skip the automatic initialization of the database and do whatever you want.
Record a small hole in spring boot about database connection pooling
Environment: spring boot 1.5, JDK1.8
Application.properties configuration # driver configuration information spring.datasource.url = jdbc:mysql://127.0.0.1:3306/mealsystem?useUnicode=true&characterEncoding=utf-8spring.datasource.username = rootspring.datasource.password = 123456spring.datasource.driverClassName = com.mysql.jdbc.Driver# connection pool configuration information spring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=20spring.datasource.maxWait=60000spring.datasource.timeBetweenEvictionRunsMillis=60000spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1 FROM DUALspring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=falsespring.datasource.poolPreparedStatements=truespring.datasource.maxPoolPreparedStatementPerConnectionSize=20spring.datasource.filters=stat Wall,log4jspring.datasource.connectionProperties=druid.stat.mergeSql=true Druid.stat.slowSqlMillis=5000 first finds this class org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder and makes a breakpoint public DataSource build () {Class in the following source code