我有一个Spring Batch应用程序,它每10分钟运行一次。它从REST API获取一些数据,然后将这些数据保存在数据库中。
好吧,我的问题在哪里?
有时候,数据库(Oracle)可能会重新启动或脱机(实际上不知道)。但是应用程序似乎没有重新连接到数据库。它只是处于空闲模式。
Spring Boot:2.1.2。发布
application.yml看起来像这样:
app:
database:
jdbc-url: jdbc:oracle:thin:@<host>:<port>:<db>
username: <username>
password: <password>
driver-class-name: oracle.jdbc.OracleDriver
options:
show-sql: true
ddl-auto: none
dialect: org.hibernate.dialect.Oracle12cDialect
然后,我像这样配置数据源:
public DataSource dataSource() {
HikariConfig configuration = new HikariConfig();
configuration.setJdbcUrl(properties.getJdbcUrl());
configuration.setUsername(properties.getUsername());
configuration.setPassword(properties.getPassword());
configuration.setDriverClassName(properties.getDriverClassName());
configuration.setLeakDetectionThreshold(60 * 1000);
return new HikariDataSource(configuration);
}
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("xxx.xxx.xx");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
Properties additionalProperties = properties();
em.setJpaProperties(additionalProperties);
return em;
}
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
private Properties properties() {
Properties additionalProperties = new Properties();
additionalProperties.setProperty("hibernate.hbm2ddl.auto", properties.getOptions().getDdlAuto());
additionalProperties.setProperty("hibernate.dialect", properties.getOptions().getDialect());
additionalProperties.setProperty("hibernate.show_sql", properties.getOptions().getShowSql());
return additionalProperties;
}
说实话,如果在配置中做错了什么,我不确定。
谢谢!
答案 0 :(得分:0)
您应通过maxLifetime来配置setMaxLifetime 30分钟
configuration.setMaxLifetime(108000);
属性控制池中连接的最大生存期。 连接超时后,即使最近使用过,也会从池中退出。使用中的连接永远不会退出,只有在空闲时才会删除。
我们强烈建议设置此值,并且该值至少应比任何数据库或基础结构施加的连接时间限制短30秒。
默认情况下,Oracle不强制连接的最长生存期