gpt4 book ai didi

java - 在第二个数据源关闭的情况下运行 Spring Boot 应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 09:56:03 26 4
gpt4 key购买 nike

我正在使用 Spring Boot 1.5.15、HikariCP 3.2.0 和 2 个数据库、MSSQL 和 Oracle。然后 yml 文件中的 jdbc 凭据字符串正确,应用程序启动成功。此外,如果在应用程序工作期间连接失败,它也可以正常工作,并且只需自动重新连接到数据库即可。但如果第二个 Oracle 数据库出现故障或类似情况,我需要能够启动应用程序。因此,然后我更改 jdbc 字符串并尝试启动应用程序,但失败了

com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool:

我搜索网络并找到不同的 HikariCP 属性或 spring.datasource 属性并尝试它们,但没有帮助。您能帮我解决这个问题吗?

我使用 yml 文件配置它:

spring:
datasource:
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver:***
username: ***
password: ***
type: com.zaxxer.hikari.HikariDataSource
jpa:
hibernate:
ddl-auto: none
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
show-sql: true
properties:
dialect: org.hibernate.dialect.SQLServer2012Dialect
database: default
jackson:
date-format: com.fasterxml.jackson.databind.util.StdDateFormat
logging:
config: classpath:logback-remote.xml
file: /usr/share/tomcat/app.log
pqa:
integration:
datasource:
driverClassName: oracle.jdbc.OracleDriver
url: jdbc:oracle:***
username: ***
password: ***
type: com.zaxxer.hikari.HikariDataSource

还有 Java 配置,我的两个数据源代码

MyApplication.class

@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer{

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}

public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}

ApplicationConfig.class

@Configuration
@Import({
ControllerConfig.class,
PersistenceConfig.class,
PlrIntegrationConfig.class,
SecurityConfig.class
})
public class ApplicationConfig {
}

持久化配置.java

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
basePackages = {"com.my.app.repository"})
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class PersistenceConfig {

@Autowired
private Environment env;

@Value("${spring.datasource.url}")
private String jdbcUrl;
@Value("${spring.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;

@Primary
@Bean(name = "dataSource")
public DataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setDriverClassName(driverClassName);
config.setJdbcUrl(jdbcUrl);
config.setUsername(username);
config.setPassword(password);
config.addDataSourceProperty("minimum-idle", "8");
config.addDataSourceProperty("maximum-pool-size", "100");
config.addDataSourceProperty("idle-timeout", "10000");
config.addDataSourceProperty("connection-timeout", "10000");
config.addDataSourceProperty("max-lifetime", "120000");
HikariDataSource dataSource = new HikariDataSource(config);
return dataSource;
}

@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource) {


return builder
.dataSource(dataSource)
.packages("com.my.app.entity")
.properties(additionalJpaProperties())
.persistenceUnit("app")
.build();
}

private Map<String, ?> additionalJpaProperties() {
Map<String, String> jpaProperties = new HashMap<>();
jpaProperties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
jpaProperties.put("hibernate.dialect", env.getProperty("spring.jpa.properties.dialect"));
jpaProperties.put("hibernate.show_sql", env.getProperty("spring.jpa.show-sql"));
jpaProperties.put("hibernate.format_sql", env.getProperty("spring.jpa.properties.hibernate.format_sql"));
return jpaProperties;
}

@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

@Bean
public AuditorAware<String> auditorProvider(){
return new AuditorAwareImpl();
}
}

PlrIntegrationConfig.class

@Configuration
@ComponentScan(value = {
"com.my.app.integration.plr.job",
})
@Import({PlrIntegrationPersistenceConfig.class})
public class PlrIntegrationConfig {
}

PlrIntegrationPersistenceConfig.class

@EnableJpaRepositories(entityManagerFactoryRef = "plrInEntityManagerFactory",
transactionManagerRef = "plrInTransactionManager",
basePackages = {"com.my.app.integration.plr.persistence.repository"})
@Configuration
public class PlrIntegrationPersistenceConfig {

@Value("${pqa.integration.datasource.url}")
private String jdbcUrl;
@Value("${pqa.integration.datasource.driverClassName}")
private String driverClassName;
@Value("${pqa.integration.datasource.username}")
private String username;
@Value("${pqa.integration.datasource.password}")
private String password;

@Bean(name = "plrInDataSource")
public DataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setDriverClassName(driverClassName);
config.setJdbcUrl(jdbcUrl);
config.setUsername(username);
config.setPassword(password);
config.addDataSourceProperty("minimum-idle", "8");
config.addDataSourceProperty("maximum-pool-size", "100");
config.addDataSourceProperty("idle-timeout", "10000");
config.addDataSourceProperty("connection-timeout", "10000");
config.addDataSourceProperty("max-lifetime", "120000");
HikariDataSource dataSource = new HikariDataSource(config);
return dataSource;
}

@Bean(name = "plrInEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean plrInEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("plrInDataSource") DataSource dataSource) {
LocalContainerEntityManagerFactoryBean plrInEMFBean = builder.dataSource(dataSource)
.packages("com.my.app.integration.plr.persistence.entity")
.persistenceUnit("plrIn")
.build();
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "org.hibernate.dialect.Oracle12cDialect");
plrInEMFBean.setJpaPropertyMap(properties);
return plrInEMFBean;
}

@Bean(name = "plrInTransactionManager")
public PlatformTransactionManager plrInTransactionManager(@Qualifier("plrInEntityManagerFactory") EntityManagerFactory plrInEntityManagerFactory) {
return new JpaTransactionManager(plrInEntityManagerFactory);
}
}

更新:已修复,需要添加

config.setInitializationFailTimeout(-1L);

在第二个数据源配置中

最佳答案

已修复,需要添加

config.setInitializationFailTimeout(-1L);

在第二个数据源配置中

关于java - 在第二个数据源关闭的情况下运行 Spring Boot 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54140490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com