0

I´m using Spring Batch in a project with two data sources, one for Spring Batch tables and other for business logic (oracle).

To configure job I have a implementation of BatchConfigurer where I set up data source for spring batch:

    public DataSource batchDataSource() {
    return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
            .addScript("org/springframework/batch/core/schema-h2.sql").build();
    }

To configure oracle db, I'm using application.properties:

#Oracle settings
spring.datasource.url=jdbc:oracle:thin:@ip/domain
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

I have the following error when there is not conexion to oracle db:

    org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.batch.BatchDataSourceScriptDatabaseInitializer]: Factory method 'batchDataSourceInitializer' threw exception; nested exception is java.lang.IllegalStateException: Unable to detect database type
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
    at com.app.app.App.main(App.java:20)
Caused by: java.lang.IllegalStateException: Unable to detect database type
    at org.springframework.util.Assert.state(Assert.java:76)
    at org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver.determinePlatform(PlatformPlaceholderDatabaseDriverResolver.java:132)
    at org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver.lambda$resolveAll$0(PlatformPlaceholderDatabaseDriverResolver.java:96)
    at org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver.resolveAll(PlatformPlaceholderDatabaseDriverResolver.java:121)
    at org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver.resolveAll(PlatformPlaceholderDatabaseDriverResolver.java:96)
    at org.springframework.boot.autoconfigure.batch.BatchDataSourceScriptDatabaseInitializer.resolveSchemaLocations(BatchDataSourceScriptDatabaseInitializer.java:86)
    at org.springframework.boot.autoconfigure.batch.BatchDataSourceScriptDatabaseInitializer.getSettings(BatchDataSourceScriptDatabaseInitializer.java:73)
    at org.springframework.boot.autoconfigure.batch.BatchDataSourceScriptDatabaseInitializer.<init>(BatchDataSourceScriptDatabaseInitializer.java:48)
    at org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration$DataSourceInitializerConfiguration.batchDataSourceInitializer(BatchAutoConfiguration.java:116)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 21 common frames omitted

Why I have the error "Failed to instantiate [org.springframework.boot.autoconfigure.batch.BatchDataSourceScriptDatabaseInitializer]:" if oracle datasource is not used for spring batch ?

Is it possible to prevent the execution from stopping when there is a connection error in db (business logic) ?

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
Marcos
  • 25
  • 6
  • `Is it possible to prevent the execution from stopping when there is a connection error in db (business logic) ?`: for these cases, failing-fast is better IMO. I would fix the configuration issue rather than preventing the execution from stopping. You can use a custom datasouce by providing a bean of type `BatchConfigurer` and override `getDataSource`, see https://stackoverflow.com/questions/25540502/use-of-multiple-datasources-in-spring-batch. – Mahmoud Ben Hassine Apr 11 '23 at 11:01

2 Answers2

0

If application.properties has:

spring.datasource.url=
spring.datasource.driverClassName=
spring.datasource.username=
spring.datasource.password=

Spring Batch first search datasource config on this properties although you have different datasource in your job config:

    public DataSource batchDataSource() {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
        .addScript("org/springframework/batch/core/schema-h2.sql").build();
    }

If you have other datasource use other name than spring, like customdb.datasource=

Solution: Change name properties of business datasource.

Marcos
  • 25
  • 6
0

Sometimes this error is about using incompatible versions on JDBC Connector, Database and Spring Boot. Try to upgrade your connector dependency inside pom.xml/build.gradle to something compatible with you Database Service Instance and SpringBoot too.

For example: using org.springframework.boot:2.7.3, mysql:mysql-connector-java:8.0.19 and MySQL service on version 8.0 a code similar to yours will be fine. But downgrading MySQL connector to mysql:mysql-connector-java:8.0.18 the console shows up the same error that you are facing: "Unable to detect database type".