Spring Boot-重新启动后重新连接到数据库 [英] Spring Boot - Reconnect to a database after its restart

查看:581
本文介绍了Spring Boot-重新启动后重新连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Batch应用程序,该应用程序每10分钟运行一次.它从REST API获取一些数据,然后将这些数据保存在数据库中.

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?

有时数据库(Oracle)可能会重新启动或脱机(不知道,实际上).但是应用程序似乎没有重新连接到数据库.它只是处于空闲模式.

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.发布

Spring Boot: 2.1.2.RELEASE

application.yml看起来像这样:

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.

谢谢!

推荐答案

您应该配置maxLifetime .html#setMaxLifetime-long-"rel =" nofollow noreferrer> setMaxLifetime 30分钟

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.

我们强烈建议设置此值,并且该值至少应比任何数据库或基础结构施加的连接时间限制少30秒.

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

默认情况下,Oracle不强制连接的最长生存期

by default Oracle does not enforce a max lifetime for connections

这篇关于Spring Boot-重新启动后重新连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆