Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use springboot not to automatically initialize database connection pool

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report