2017-09-14 99 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