0

I have a spring-boot application with 3 webservices that access to two different databases declared in application.properties file.

spring.datasource.url = jdbc:oracle
spring.datasource.username = aa
spring.datasource.password = aa
spring.seconddatasource.url = jdbc:oracle2
spring.seconddatasource.username = aa
spring.seconddatasource.password = aa

When I run the application, if a connection fails it ends the whole application even if one of the connections works.

I need to connect to all databases, and if a database isn't working, try to reconnect, but the application can not end.

I have tried these configurations but with no success :

testOnBorrow=true 
validationQuery=SELECT 1
timeBetweenEvictionRunsMillis = 60000

Also I have a DataBaseConfig.java with

@Configuration
public class DataBaseConfig {


@Bean(name = "mysqlDb")
@Primary
@ConfigurationProperties(prefix="spring.datasource") 
public DataSource mysqlDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "sqliteDb")
@ConfigurationProperties(prefix="spring.secondDatasource") 
public DataSource sqliteDataSource() {
    return DataSourceBuilder.create().build();
}


 @Bean(name = "cli")
    public JdbcTemplate slaveJdbcTemplate(@Qualifier("mysqlDb") DataSource datasource) {
         return new JdbcTemplate(datasource); 
    }

    @Bean(name = "usr")
    @Primary
    public JdbcTemplate masterJdbcTemplate(@Qualifier("sqliteDb") DataSource secondDatasource) {
        return new JdbcTemplate(secondDatasource);
    }   
}

Console errors :

Unable to create initial connections of pool
HHH000342: Could not obtain connection to query metadata : ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist
org.springframework.beans.factory.BeanCreationException: Error creating bean        with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
jesicadev18
  • 2,942
  • 5
  • 22
  • 39

1 Answers1

0

Create the connection pools programmatically rather than letting Spring Boot autoconfigure them for you. Then you can handle any errors in building the datasource in your code. See:

Configure DataSource programmatically in Spring Boot

Alternatively create a single connection datasource at runtime rather than at boot time, and add retry logic in the event of an error (have a look at Spring Retry)

PaulNUK
  • 4,774
  • 2
  • 30
  • 58