Springboot data source configuration, Connection pool configuration, source analysis, how to select the connection pool

Source: Internet
Author: User
Tags null null static class volatile
Springboot DirectorySpring Boot Source Depth Analysis Springboot data source configuration, Connection pool configuration, source analysis, how to select the connection pool spring boot Multiple data source configuration, multiple data source transactions

Before the Springboot how to configure MySQL and configure multiple data sources, parameters are also configured well, then springboot in the end how to choose.

Statement: The author uses the Springboot version is 1.5.3.RELEASE

Springboot Automatic assembly process is not said, directly on the core code.

Org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration This class is the core configuration class for the Springboot load data source.
Let's look at the connection pool selection:

@Configuration
    @Conditional (pooleddatasourcecondition.class)
    @ConditionalOnMissingBean ({ Datasource.class, Xadatasource.class})
    @Import ({DataSourceConfiguration.Tomcat.class, DataSourceConfiguration.Hikari.class,
            DataSourceConfiguration.Dbcp.class, DataSourceConfiguration.Dbcp2.class,
            DataSourceConfiguration.Generic.class})
    @SuppressWarnings (" Deprecation ")
    protected static class Pooleddatasourceconfiguration {

    }

Pooleddatasourceconfiguration The above annotations include a mainstream connection pool, such as Tomcat connection pool, DBCP,DBCP2, and so on. Which connection pool do you choose? ( Select a different connection pool, application.properties configured differently )
Step down:
Enter Pooleddatasourcecondition
Enter Pooleddatasourceavailablecondition

Static Class Pooleddatasourceavailablecondition extends Springbootcondition {@Override public Conditiono Utcome Getmatchoutcome (conditioncontext context, Annotatedtypemetadata metadata) {Conditionme Ssage.
            Builder message = conditionmessage. Forcondition ("Pooleddatasource"); if (Getdatasourceclassloader (context)!= null) {return conditionoutcome. Match (Me
            ssage.foundexactly ("Supported DataSource"));
        Return conditionoutcome. NoMatch (Message.didnotfind ("Supported DataSource"). Atall ()); }/** * Returns the class loader for the {@link DataSource} class.
         Used to ensure that * the driver class can actually is loaded by the data source. * @param context of the condition context * @return The class loader * * Private ClassLoader GetData Sourceclassloader (Conditioncontext Context) {class<?> Datasourceclass = new Datasourcebuilder (Context.getclassloader ())
        . Findtype ()/* Find the source/return (Datasourceclass = = null null:dataSourceClass.getClassLoader ()); }

    }

Enter the Findtype method:

Public class<? Extends Datasource> Findtype () {
        if (this.type!= null) {return
            this.type;
        }
        for (String name:data_source_type_names) {
            try {return
                (class<? extends datasource>) classutils.forname (Name,
                        this.classloader);
            }
            catch (Exception ex) {
                //Swallow and Continue
            }} return
        null;
    }

There is an array of constants in it:

private static final string[] Data_source_type_names = new string[] {
            "Org.apache.tomcat.jdbc.pool.DataSource", c15/> "Com.zaxxer.hikari.HikariDataSource",
            "Org.apache.commons.dbcp.BasicDataSource",//deprecated
            " Org.apache.commons.dbcp2.BasicDataSource "};

The array is traversed in the Findtype () method, and the connection pool of which class is used from the time it is loaded to which class.
Depending on the classes in different projects, which can result in a different connection pool, it is best to add a breakpoint here and see for yourself which connection pool is ultimately selected

The image above shows that the author uses a Tomcat connection pool: Org.apache.tomcat.jdbc.pool.DataSource

Enter the Org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer class, which has the Init method, which is loaded when it is started. The properties file and the DataSource configuration are done here.

@PostConstruct public
    void init () {
        if (!this.properties.isinitialize ()) {
            Logger.debug ("initialization Disabled (not running DDL scripts) ");
            return;
        }
        if (This.applicationContext.getBeanNamesForType (Datasource.class, False,
                false). length > 0) {
                // Load the DataSource class to confirm which connection pool to use,
            This.datasource = This.applicationContext.getBean (Datasource.class);
        }
        if (This.datasource = null) {//view DataSource configuration information here
            logger.debug ("No DataSource found not initializing");
            return;
        }
        Runschemascripts ();
    }

Add a breakpoint here to see which connection pool is being used and whether our configuration is in effect.

OK, next is to look at the source code Org.apache.tomcat.jdbc.pool.DataSource or official documents, to configure the parameters.
Configuration information can refer to these two classes:

public class Datasourceproxy implements Poolconfiguration {
    private static final log = Logfactory.getlog (datasour Ceproxy.class);
    Link configuration
    protected volatile connectionpool pool = null;
    Connection pool configuration
    protected volatile poolconfiguration poolproperties = null;

    Omit code ...
}
reference Documentation

Test DB connection, max wait time, DB test
Use Druid Connection pool to bring the pit testonborrow=false

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.