有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpring数据,MySQL,连接在8小时不活动后终止

我面临一个问题,在一段时间的不活动之后,我的数据源bean将崩溃。我的问题是如何重新实例化在应用程序启动时遇到的数据源bean

下面是我们如何在启动时设置bean

@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource dataSource(){
    byte[] encryptedFile = fileRetriever.getFile(bucket, key);
    String unencryptedJson = fileDecrypter.decryptFile(encryptedFile);
    JSONParser parser = new JSONParser();
    JSONObject jsonObject = null;
    try{
        jsonObject = (JSONObject) parser.parse(unencryptedJson);
    }catch (Exception ex){
        log.error(ex.getMessage());
    }

    String password = (String)jsonObject.get("password");

    DataSource ds = DataSourceBuilder
            .create()
            .url(url)
            .username(userName)
            .password(password)
            .build();

    return ds;

}

这个类还有一个@Configuration注释

我们还有其他没有这个问题的应用程序,这些应用程序在不活动后需要跳转服务,但它们也没有以这种方式设置数据源,并且在application.property文件中指定了所有细节

我添加了一个自定义的健康检查,它使用一个每隔30秒点击一次的存储库,这样应该可以保持数据源bean的活动,但万一它真的崩溃了,我需要一种方法来重新创建它

提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    我假设boot正在为您配置数据源。在本例中,由于您使用的是MySQL,因此可以将以下内容添加到应用程序中。高达1.3%的房产

    spring.datasource.test-on-borrow=true
    spring.datasource.validationQuery=SELECT 1
    
  2. # 2 楼答案

    可能被认为是池数据源连接器。看看ApacheDBCB2

    下面是我的一个示例,它至少保持10个空闲,并根据需要从池中增加

    private static DataSource createDataSource(String url, String userName, String passwrd) throws Exception {
        Class.forName(DRIVER).newInstance();
    
        Properties props = new Properties();
        props.setProperty("user", userName);
        props.setProperty("password", passwrd);
    
        //Create a connection factory that the pool will use to create connections
        ConnectionFactory cf = new DriverManagerConnectionFactory(url, props);
        //Create the poolable connection factory 
        PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, null);
        pcf.setValidationQuery("SELECT 1");
        pcf.setDefaultAutoCommit(true);
    
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMinIdle(10);
        poolConfig.setMaxTotal(100);
    
        AbandonedConfig abandonConfig = new AbandonedConfig();
        abandonConfig.setRemoveAbandonedTimeout(60);
        abandonConfig.setLogAbandoned(false);
        abandonConfig.setRemoveAbandonedOnBorrow(true);
        abandonConfig.setRemoveAbandonedOnMaintenance(true);
    
        //Create the pool of connections
        GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(pcf, poolConfig);
        connectionPool.setTestOnBorrow(true);
        connectionPool.setTestWhileIdle(true);
        connectionPool.setTimeBetweenEvictionRunsMillis(10000);
        connectionPool.setMinEvictableIdleTimeMillis(1000);
        connectionPool.setAbandonedConfig(abandonConfig);
        pcf.setPool(connectionPool);
    
        //Pooling data source itself
        PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
        return dataSource;
      }
    

    apache dbcb2的Maven依赖项

            <!  Database connection pools  >
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-dbcp2</artifactId>
                <version>2.1.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
                <version>2.4.2</version>
            </dependency>