Alibaba druid the bug in Springboot start AutoConfig

Source: Internet
Author: User

* Background

* Discovery and Analysis process

* Summary


Background

Recently, when using Alibaba Druid to make a multi-data source connection, inadvertently found a small bug, has been submitted to GitHub issue official already fix. Issue Address: https://github.com/alibaba/druid/issues/1796


Discovery, Analysis process

The Java Development Framework we are using is packaged well. The framework of the data source support is the master, slave architecture, it can be a group of multiple from the data source, the internal automatic master-slave writing, query switching.


We are now in the. NET Java process, the new Java system needs to connect two data sources, the MySQL data source is connected by default, but sometimes it is necessary to query the SQL Server data source to obtain some compatibility data.

Therefore, when the second data source is configured, the system load is an error.


We use the Springboot framework, DataSource config is based on springboot properties. The configuration is then used to automatically druid the creation of the Daasource bean. It looks like there's no problem.

@Bean (name = "DataSource") @ConfigurationProperties (prefix = "ecommon.order.druid") public DataSource Getorderdataso    Urce () {return new Druiddatasource (); }

If it is not springboot, it will usually initialize all of its properties. Load the configuration from the configuration file, and then manually set the Druiddatasource bean.


Without much thought, it was used directly in this way. But at startup, when the bean is initialized, an error is made.

' Maxevictableidletimemillis ' threw exception; Nested exception is Java.lang.IllegalArgumentException:maxEvictableIdleTimeMillis must be grater than Minevictableidletimemillis


Probably means that the ' maxevictableidletimemillis ' maximum survival time must be greater than ' minevictableidletimemillis ' minimum survival time.


The first reaction must have been misconfigured, check the configuration.

# #一个连接在池中最小生存的时间 (ms) ecommon.order.druid.minevictableidletimemillis=300000# #一个连接在池中最大生存的时间 (MS) ecommon.order.druid.maxevictableidletimemillis=600000

It seems to be true, and then under Debug, the problem also arises. The Minevictableidletimemillis property and the Maxevictableidletimemillis property seem to have no problem in the order. Try the heart, the two attributes before and after the order of exchange, the problem still occurs.


Blinded State ~_~.


Feel this problem is a bit strange, time matters, directly find the place of error, source tracking.

On the GitHub search Alibaba Druid home page, directly Gith clone down and navigate to the location of the error tip.


There are two places in the global search that have this exception throw.

One: Init method

public void Init () throws SQLException {if (Maxevictableidletimemillis < Minevictableidletimemillis) {            throw new SQLException ("Maxevictableidletimemillis must be grater than Minevictableidletimemillis"); }

For readability, I deleted the code in init that was useless for our analysis of the problem.


Two: Setmaxevictableidletimemillis method

Public void setmaxevictableidletimemillis (Long maxevictableidletimemillis)  {         if  (MAXEVICTABLEIDLETIMEMILLIS&NBSP;&LT;&NBSP;1000&NBSP;*&NBSP;30)  {            log.error (" maxevictableidletimemillis should be greater than 30000 ");         }                 if  (Maxevictableidletimemillis < minevictableidletimemillis)  {             throw new illegalargumentexception (" Maxevictableidletimemillis must be grater than minevictableidletimemillis ");         }                 this.maxevictableidletimemillis = maxevictableidletimemillis;    } 

The logic of both methods is simple, and when Init initializes, the values of the two bean properties are checked. Setmaxevictableidletimemillis, there is a check when setting this maximum survival time. If your maximum survival time is less than the minimum survival time, direct error.


So directly in these two places breakpoints, and then debug, in tracking down the value of the variable basically know where the problem is.


Through debug, it is found that the direct new Druiddatasource () uses a data source and does not go to the Init method. Does not enter the place where the error is triggered. It seems that my code path should not produce this check. Keep running and see what happens with the Setmaxevictableidletimemillis method.


The problem is found, and the value of the Minevictableidletimemillis property is 1800000 when the Setmaxevictableidletimemillis method is performed.


Look at where this value came from.

Protected volatile long minevictableidletimemillis = Default_min_evictable_idle_time_millis;
public static final Long Default_min_evictable_idle_time_millis = 1000L * 60L * 30L;

The Minevictableidletimemillis property has a default value. The unit is MS (millisecond), so the default value here is 30m (minute) minutes.


We configured 600000ms, so it's smaller than 1800000. But this is strange, we clearly set the Minevictableidletimemillis parameter. Why did not work, suddenly thought is not the order problem.


Then proceed to debug, bypassing this check to see if the Setminevictableidletimemillis method executes after the Setmaxevictableidletimemillis method, causing this check and configuration order to conflict.


Debug down, that's really the problem. Then more curious, we are now using this framework to help us encapsulate the druid of the time is how to deal with, by looking at the framework of the source code, there is a hashcode sort, solve the problem. (Sure enough, the old driver, master) here will not unfold.


Then if we solve this problem, it is very simple, it is very simple to know where the problem is going around.


First: The configuration of the Minevictableidletimemillis, Maxevictableidletimemillis get in

@Data @equalsandhashcode@configurationproperties (prefix = "Ecommon.order.druid") public class Sqlserverdruidconfig {    Private Long Minevictableidletimemillis; Private Long Maxevictableidletimemillis;}

Two: Then you set these two properties first, Springboot AutoConfig will not be wrong

@Autowired     private sqlserverdruidconfig druidconfig;    @ Bean (name =  "DataSource")      @ConfigurationProperties (prefix =  " Ecommon.order.druid ")     public datasource getorderdatasource ()  {         druiddatasource datasource = new druiddatasource         /**setminevictableidletimemillis need to set */    first      datasource.setminevictableidletimemillis ( Druidconfig.getminevictableidletimemillis ());         Datasource.setmaxevictableidletimemillis (Druidconfig.getmaxevictableidletimemillis ());         return dataSource;    }     @Bean      public sqlserverdruidconfig getdruidconfig () &NBSP;{&NBsp;       return new sqlserverdruidconfig ();     }

The problem here is the end of the analysis. This little Bug,alibaba druid has been fix.

If you are interested in how to fix it, please see Druid for the code for Autoconfiger repair:

Https://github.com/lihengming/druid/commit/ca13e8ff5a78c83f953fa8fb320c56be223219e1


Summarize

All of a sudden, you have benefited from the benefits of open source. Can participate in the use together, participate in discovering problems together, participate in Fixbug. This is perhaps the core technical value behind the great works of art like Linux and Git.


GitHub Address: Https://github.com/Plen-wang

This article is from the "King Qingyue" blog, please be sure to keep this source http://wangqingpei557.blog.51cto.com/1009349/1945480

Alibaba druid the bug in Springboot start AutoConfig

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.