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 solve the problem that springboot configuration druid data source is not effective

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

Share

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

Today, I will talk to you about how to solve the problem that the springboot configuration druid data source is not effective. Many people may not know much about it. In order to make you understand better, the editor summarized the following content for you. I hope you can get something according to this article.

Pom file

If your springboot project is going to use druid, these three dependencies are essential:

Com.alibaba druid-spring-boot-starter 1.1.10 com.alibaba druid 1.1.12 log4j log4j 1.2.17

The first hole I stepped on was right here. I didn't introduce the druid-spring-boot-starter dependency at that time, so there was a problem during the test, and finally I looked up the data to solve it. The pit below is even more outrageous.

Trampling experience and solution

At first, my yml configuration file was written like this:

Spring: datasource: username: root password: bugeinikan url: jdbc:mysql://localhost:3306/2021summer?useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true

There seems to be nothing wrong with it, and idea didn't report it wrong.

Then we add the data source to the container with the DruidConfig class code as follows:

@ Configurationpublic class DruidConfig {@ ConfigurationProperties (prefix = "spring.datasource") @ Bean public DataSource druidDataSource () {return new DruidDataSource ()}

Write the test class, and the test program class code is as follows:

@ SpringBootTestclass ApplicationTests {@ Autowired DataSource dataSource; @ Test void contextLoads () throws SQLException {System.out.println (dataSource.getClass ()); / / get a connection Connection connection = dataSource.getConnection (); System.out.println (connection); DruidDataSource druidDataSource = (DruidDataSource) dataSource; System.out.println ("maximum number of connections to druidDataSource data sources:" + druidDataSource.getMaxActive ()) System.out.println ("number of druidDataSource data source initialization connections:" + druidDataSource.getInitialSize ()); / / close connection connection.close ();}}

Output result:

Class com.alibaba.druid.pool.DruidDataSource

2021-09-24 16 dataSource-1 12 INFO 4408-[main] com.alibaba.druid.pool.DruidDataSource: {dataSource-1} inited

Com.mysql.cj.jdbc.ConnectionImpl@7b96de8d

Maximum number of connections to druidDataSource data sources: 8

Number of druidDataSource data source initialization connections: 0

The program looks fine, but the output is different from what you set up. Let's interrupt a little bit and take a look at it in detail:

As you can see, the username, password, url, and driverClass configurations for the data source are all in effect in the yml file.

Look further down:

In the yml file, the maxActive and maxWait are clearly set to 20 and 60000, but the default properties are shown here, which indicates that the spring.datasource.druid configuration we wrote in the yml file does not take effect. With a try, I have made the following modifications to the yml file:

Spring: datasource: username: root password: bugeinikan url: jdbc:mysql://localhost:3306/2021summer?useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true

At this point, idea reported to me that it could not be parsed, as shown in the figure:

Regardless of it, the dead horse has to be a live horse doctor, we start the test program and run the results:

Class com.alibaba.druid.pool.DruidDataSource

2021-09-24 16 com.alibaba.druid.pool.DruidDataSource 1915 INFO 11428-[main] com.alibaba.druid.pool.DruidDataSource: {dataSource-1} inited

Com.mysql.cj.jdbc.ConnectionImpl@67e25252

Maximum number of connections to druidDataSource data sources: 20

Number of druidDataSource data source initialization connections: 5

This time the output is the same as our configuration, in the later breakpoint test, all the properties are the same as the configuration.

I have to say, I was really cheated by idea this time. At first, I thought it was a failure of injection, and then I checked a lot of information. My springboot version is 2.5.4. I hope this article will help those who step in the pit like me.

After reading the above, do you have any further understanding of how to solve the problem that the springboot configuration druid data source does not work? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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: 221

*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