Deep Spring Boot: How to troubleshoot cannot determine embedded database driver class for database type NONE

Source: Internet
Author: User
Tags null null
It 's written in front .

This demo shows you how to troubleshoot a common spring boot autoconfiguration error step-by-step.

Https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-database-type-NONE debugging and Troubleshooting cannot determine Embedded database driver class for database type NONE error

To import the project into the IDE and start the application directly, the exception information thrown is:

Error starting ApplicationContext. To display the auto-configuration, re-run your application with ' Debug ' enabled.
2017-11-29 14:26:34.478 ERROR 29736---[           main] o.s.b.d.loggingfailureanalysisreporter   :

***************
application FAILED to START
***************************

Description:

cannot determine Embedded database driver class for database type NONE

Action:

If your want a embedded database please put a Suppo RTed one on the classpath. If you are have database settings to is loaded from a particular profiles you'll need to active it (no profiles are currently Active).

In fact, there are two ideas, direct Google search cannot determine embedded database driver class for database type NONE, you can find a solution.

The second way, a careful look at the log content, you can find that there is to display the "Auto-configuration" re-run your application with ' debug ' enabled.

If you search this, you can find relevant information on spring's official website: https://docs.spring.io/spring-boot/docs/current/reference/html/ Using-boot-auto-configuration.html

Is the user configured debug This switch, will be the Auto-configuration related information printed out.

Familiar with spring's environment variable injection, there are several ways to open this: adding--debug to the args in application.properties debug=true through-ddebug=true Increase the information after the debug switch

After adding the debug switch, you can see that the error stack is printed:

2017-11-29 14:33:08.776 DEBUG 29907---[main] o.s.b.d.loggingfailureanalysisreporter:application failed to Start due to a exception org.springframework.boot.autoconfigure.jdbc.datasourceproperties$ Datasourcebeancreationexception:cannot Determine embedded database driver class for database type NONE. If you are want an embedded database, put a supported one on the classpath. If you are have database settings to is loaded from a particular profiles you'll need to active it (no profiles are currently
    Active). At Org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName ( datasourceproperties.java:245) ~[spring-boot-autoconfigure-1.4.7.release.jar:1.4.7.release] at Org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder ( datasourceproperties.java:182) ~[spring-boot-autoconfigure-1.4.7.release.jar:1.4.7.release] at Org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSourcE (datasourceconfiguration.java:42) ~[spring-boot-autoconfigure-1.4.7.release.jar:1.4.7.release] at Org.springframework.boot.autoconfigure.jdbc.datasourceconfiguration$tomcat.datasource ( datasourceconfiguration.java:53) ~[spring-boot-autoconfigure-1.4.7.release.jar:1.4.7.release] at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native method) ~[na:1.8.0_112]

The code that throws the exception is:

    /** * Determine the driver to use based on this configuration and the environment. * @return the driver to use * @since 1.4.0/public String determinedriverclassname () {if (Stringu Tils.hastext (This.driverclassname)) {assert.state (driverclassisloadable (), "Cannot load D
            River class: "+ This.driverclassname";
        return this.driverclassname;

        } String driverclassname = null; if (Stringutils.hastext (This.url)) {driverclassname = Databasedriver.fromjdbcurl (this.url). GetDriverClassName
        (); } if (! Stringutils.hastext (Driverclassname)) {driverclassname = This.embeddedDatabaseConnection.getDriverClassName ()
        ; } if (! Stringutils.hastext (Driverclassname)) {throw new Datasourcebeancreationexception (this.embeddeddatabaseconnect
        Ion, This.environment, "Driver class"); Return Driverclassname; }

Can be seen is not found DataSource driver class, and then throw the datasourcebeancreationexception.

One solution is to add some DataSource driver class to Maven dependencies.

But applying your own code does not use DataSource, where it causes spring boot to create a DataSource object. where will cause spring boot to create datasource

From the exception stack, you can find the Datasourceconfiguration$tomcat class, and then find its reference, Can find that it is being org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.PooledDataSourceConfiguration Imported by import.

    @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})
    protected static class pooleddatasourceconfiguration {

    }

So how did the pooleddatasourceconfiguration come into effect? @conditional (Pooleddatasourcecondition.class) can be seen from the code.

Then look at the specific implementation of Pooleddatasourcecondition:

    /**
     * {@link anynestedcondition} that checks, either {@code Spring.datasource.type}
     * is set or {@link Pooledd Atasourceavailablecondition} applies.
     *
    /Static class Pooleddatasourcecondition extends Anynestedcondition {

        pooleddatasourcecondition () {
            Super (configurationphase.parse_configuration);
        }

        @ConditionalOnProperty (prefix = "Spring.datasource", name = "type")
        static class Explicittype {

        }

        @ Conditional (pooleddatasourceavailablecondition.class)
        static class Pooleddatasourceavailable {

        }

    }

Pooleddatasourcecondition introduced @conditional (pooleddatasourceavailablecondition.class):

    /** * {@link Condition} to test if a supported connection the pool is available. */Static Class Pooleddatasourceavailablecondition extends Springbootcondition {@Override public Con Ditionoutcome Getmatchoutcome (conditioncontext context, Annotatedtypemetadata metadata) {Cond
            Itionmessage.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 * @returnThe class loader * * Private ClassLoader Getdatasourceclassloader (Conditioncontext context) {
            class<?> Datasourceclass = new Datasourcebuilder (Context.getclassloader ()). Findtype ();
        return (Datasourceclass = = null null:dataSourceClass.getClassLoader ()); }

    }

From the code, you can see is trying to find datasourceclass, if found, the condition is set up. Then debug, you can find the Datasourceclass is: Org.apache.tomcat.jdbc.pool.DataSource.

Then look at the Org.apache.tomcat.jdbc.pool.DataSource where the class comes from.

From the Maven dependency tree you can see that dependencies are from: Spring-boot-starter-jdbc. So the application relies on SPRING-BOOT-STARTER-JDBC, but there is no problem with the datasource caused by the configuration. Problem Solving Method

There are two kinds: if you don't use DataSource, you can remove SPRING-BOOT-STARTER-JDBC dependencies so that spring boot-related code is not triggered to automatically initialize spring boot DataSource related code.

There are two ways of banning:

Configure exclude on the main function


@SpringBootApplication (exclude = {Datasourceautoconfiguration.class, Datasourcetransactionmanagerautoconfiguration.class})

Configure in Application.properties:


Spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration, Org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
The summary application was not used to DataSource, but in Pom.xml the Spring-boot-starter-jdbc Spring-boot-starter-jdbc was introduced into the TOMCAT-JDBC, It has the pooleddatasourceconfiguration in Org.apache.tomcat.jdbc.pool.DataSource spring boot, To determine the DataSource implementation class under Classpath, try to create the DataSource bean when initializing datasourceproperties, try to detect the driver class via the JDBC URL Because the application does not configure the URL, it eventually throws cannot determine embedded database driver class in Datasourceproperties.determinedriverclassname () For database type NONE

Finally: To troubleshoot the spring boot autoconfiguration problem, you can press the exception stack, layer by layer to troubleshoot how configuration is introduced, and then troubleshoot condition specific judgment code.

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.