the springboot version is 2.5.1 When there is one datasource bean (but is extend AbstractRoutingDataSource ), meet the condition of create the bean (SqlInitializationAutoConfiguration 、DataSourceInitializationConfiguration) ,so will run the class AbstractScriptDatabaseInitializer# afterPropertiesSet, will Throw exception (because need to getConnection, but no datasource to choose ,I use the @annotation and AbstractRoutingDataSource to choose which database to use )

the solution :Add a judgment ,as follows:

DataSourceScriptDatabaseInitializer# isEmbeddedDatabase

` protected boolean isEmbeddedDatabase() {

            if(this.dataSource instanceof AbstractRoutingDataSource){
                return false; 
            }
    return EmbeddedDatabaseConnection.isEmbedded(this.dataSource);
}

`

error information:

at org.springframework.boot.jdbc.EmbeddedDatabaseConnection.isEmbedded(EmbeddedDatabaseConnection.java:184)
at org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer.isEmbeddedDatabase(DataSourceScriptDatabaseInitializer.java:64)
at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.isEnabled(AbstractScriptDatabaseInitializer.java:87)
at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.initializeDatabase(AbstractScriptDatabaseInitializer.java:74)
at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.afterPropertiesSet(AbstractScriptDatabaseInitializer.java:65)

Comment From: snicoll

@crazylulululu this method delegates to EmbeddedDatabaseConnection.isEmbedded that catches DataAccessException. In your case, it looks like a different exception is thrown but, unfortunately, the information you've shared does not provide which one.

To help us make some progress, can you please share a small sample that we can run ourselves rather than partial code and stacktraces in text? In the meantime, you can get rid of the check by opting-out for database initialization (which sounds the right call for your setup anyway). You can add spring.sql.init.mode=never in your configuration.

Comment From: wilkinsona

In 2.4 we checked for scripts and then checked to see if the database type matches the initialization mode. Those checks are reversed in 2.5.1 so the database check will be performed even if there aren't any scripts. I think we should possibly reverse the ordering in 2.5.x so that this problem only occurs if you have some initialization scripts.

Comment From: crazylulululu

@crazylulululu this method delegates to EmbeddedDatabaseConnection.isEmbedded that catches DataAccessException. In your case, it looks like a different exception is thrown but, unfortunately, the information you've shared does not provide which one.

To help us make some progress, can you please share a small sample that we can run ourselves rather than partial code and stacktraces in text? In the meantime, you can get rid of the check by opting-out for database initialization (which sounds the right call for your setup anyway). You can add spring.sql.init.mode=never in your configuration.

@snicoll , yes , I throw a different exception , use the IllegalArgumentException , so can not be catched ,Thanks .

Comment From: crazylulululu

@crazylulululu this method delegates to EmbeddedDatabaseConnection.isEmbedded that catches DataAccessException. In your case, it looks like a different exception is thrown but, unfortunately, the information you've shared does not provide which one.

To help us make some progress, can you please share a small sample that we can run ourselves rather than partial code and stacktraces in text? In the meantime, you can get rid of the check by opting-out for database initialization (which sounds the right call for your setup anyway). You can add spring.sql.init.mode=never in your configuration.

@snicoll , I have solved the problem . why not change the DataAccessException to Exception , As long as there's a exception,we can return false ,not just the DataAccessException

Comment From: wilkinsona

Thanks for getting back to us, @crazylulululu. I've opened https://github.com/spring-projects/spring-boot/issues/26925 to reverse the ordering of the checks.