Spring Boot - Reconnect to a database after its restart

不打扰是莪最后的温柔 提交于 2020-01-24 21:48:26

问题


I have an Spring Batch application, which runs every 10 minutes. It gets some data from a REST API and then it saves these data on a database.

Well, where is my problem now?

Sometimes the database (Oracle) may restart, or go offline (no idea, really). But the application doesn't seem to reconnect to the database. It just stays on an idle mode.

Spring Boot: 2.1.2.RELEASE

The application.yml looks like this:

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

and then, I configure the DataSource like this:

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

To be honest, I am not really sure, if I have done anything wrong here in the configuration.

Thank you!


回答1:


You should configure maxLifetime by setMaxLifetime for 30 minutes

 configuration.setMaxLifetime(108000);

property controls the maximum lifetime of a connection in the pool. When a connection reaches this timeout, even if recently used, it will be retired from the pool. An in-use connection will never be retired, only when it is idle will it be removed.

We strongly recommend setting this value, and it should be at least 30 seconds less than any database or infrastructure imposed connection time limit.

by default Oracle does not enforce a max lifetime for connections



来源:https://stackoverflow.com/questions/58917381/spring-boot-reconnect-to-a-database-after-its-restart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!