Stop unsuccessfull datasource configuration from shutting down springboot application

18 views Asked by At

I currently have two datasources configured in my application (following this example: https://medium.com/javarevisited/configure-multiple-data-sources-postgres-oracle-in-a-spring-boot-application-72b28a9e6ea9)

My oracle connection is the "main" connection, where it handles persistance of user data, the postgres connection is only a log agregator, the behavior I want to achieve is that, even that the postgres connection isn't successfull, I don't want my springboot application to shutdown, how would I be able to acheive that?

postgresDbConfig.java:

    @Configuration
    @EnableJpaRepositories(
    entityManagerFactoryRef = "postgresEntityManagerFactory",
    transactionManagerRef = "postgresTransactionManager",
    basePackages = { "com.verdecard.cobranca_service.postgres.repository" })
    @EnableTransactionManagement
    public class postgresDbConfig {
    @Bean(name = "postgres")
    @ConfigurationProperties(prefix = "postgres.spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "postgresEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean postgresEntityManagerFactory(
            final EntityManagerFactoryBuilder builder,
            @Qualifier("postgres") final DataSource dataSource) {
        return builder
                .dataSource(dataSource)
                .packages("com.verdecard.cobranca_service.postgres.entity")
                .persistenceUnit("postgres")
                .build();
    }
    @Bean(name = "postgresTransactionManager")
    public PlatformTransactionManager postgresTransactionManager(
            @Qualifier("postgresEntityManagerFactory") final EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

I already tried adding try/catch blocks and just printing the stacktrace of the Exception, but it didn't give me the result I wanted, my spring boot application keeps shutting down if the postgres connection isn't successfull.

0

There are 0 answers