2017-09-14 48 views
0

我有一個spring-boot應用程序,有3個webservices,可訪問在application.properties文件中聲明的兩個不同數據庫。SpringBoot管理連接池錯誤

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 

當我運行應用程序時,如果連接失敗,即使其中一個連接有效,它也會結束整個應用程序。

我需要連接到所有數據庫,並且如果數據庫不工作,請嘗試重新連接,但應用程序無法結束。

我嘗試了這些配置,但沒有成功:

testOnBorrow=true 
validationQuery=SELECT 1 
timeBetweenEvictionRunsMillis = 60000 

我也有

@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); 
    } 
} 

控制檯誤差的DataBaseConfig.java:

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] 

回答

0

創建連接池以編程方式而不是讓Spring Boot爲您自動配置它們。然後,您可以在代碼中處理構建數據源時的任何錯誤。請參閱:

Configure DataSource programmatically in Spring Boot

或者在運行時,而不是在啓動時創建一個單一的數據源連接,並在錯誤的事件中添加重試邏輯(看看春重試)

+0

我是新春天我用我的databaseConfig文件更新 – lorenag83