我有一个带有3个webservices的spring-boot应用程序,可以访问application.properties文件中声明的两个不同的数据库。
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
我还有一个带
的DataBaseConfig.java@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);
}
}
控制台错误:
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 :(得分:0)
以编程方式创建连接池,而不是让Spring Boot为您自动配置它们。然后,您可以处理在代码中构建数据源的任何错误。参见:
Configure DataSource programmatically in Spring Boot
或者在运行时而不是在引导时创建单个连接数据源,并在发生错误时添加重试逻辑(查看Spring重试)