Spring数据,MySQL,连接在8小时不活动后死亡

问题描述 投票:0回答:2

我遇到一个问题,我的数据源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保持活动状态,但是如果它确实存在,我需要一种方法来重新创建它。

提前致谢

java spring hibernate spring-data
2个回答
0
投票

我假设启动正在为您配置DataSource。在这种情况下,由于您使用的是MySQL,因此可以将以下内容添加到最多1.3的application.properties中

spring.datasource.test-on-borrow=true
spring.datasource.validationQuery=SELECT 1

0
投票

可能被认为是一个池数据源连接器。看看apache dbcb2。

这是我有一个样本,它至少保持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>
© www.soinside.com 2019 - 2024. All rights reserved.