Spring Boot应用程序中的Liquibase保持10个连接打开 [英] Liquibase in Spring boot application keeps 10 connections open

查看:74
本文介绍了Spring Boot应用程序中的Liquibase保持10个连接打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有Liquibase集成的Spring Boot应用程序来设置数据库.我们使用不同的用户来更改我们使用 application.properties 文件配置的数据库

I'm working on a Spring Boot application with Liquibase integration to setup the database. We use a different user for the database changes which we configured using the application.properties file

liquibase.user=abc
liquibase.password=xyz
liquibase.url=jdbc:postgresql://something.eu-west-1.rds.amazonaws.com:5432/app?ApplicationName=${appName}-liquibase
liquibase.enabled=true
liquibase.contexts=dev,postgres

目前,我们正在部署3个不同的微服务,并且我们注意到,对于每个正在运行的实例,Liquibase都会打开10个连接,除非停止应用程序,否则它永远不会关闭这些连接.这基本上意味着在开发中,我们经常达到Amazon RDS实例的连接限制.

We have at this moment 3 different microservices in deployment and we noticed that for every running instance, Liquibase opens 10 connections and it never closes these connections unless we stop the application. This basically means that in development we regularly hit the connection limit of our Amazon RDS instance.

目前,在开发中,Liquibase占用了74个活动连接中的40个.如果我们要为此投入生产,并且为所有微服务启用自动缩放功能,那意味着我们将不得不过度扩展数据库,以免达到任何连接限制.

Right now, in development, 40 of 74 active connections are occupied by Liquibase. If we ever want to go to production with this, having autoscaling enabled for all the microservices, that would mean we'll have to over-scale the database in order not to hit any connection limits.

有没有办法

  • 告诉liquibase不要使用10个连接的连接池
  • 告诉liquibase停止或关闭连接

到目前为止,我还没有找到有关如何执行此操作的文档.

So far I found no documentation on how to do this.

推荐答案

由于Slava的响应,我设法解决了以下数据源配置类的问题

Thanks to the response of Slava I managed to fix the problem with following datasource configuration class

@Configuration
public class LiquibaseDataSourceConfiguration {

    private static final Logger LOG = LoggerFactory.getLogger(LiquibaseDataSourceConfiguration.class);

    @Autowired
    private LiquibaseDataSourceProperties liquibaseDataSourceProperties;

    @LiquibaseDataSource
    @Bean
    public DataSource liquibaseDataSource() {
        DataSource ds =  DataSourceBuilder.create()
                .username(liquibaseDataSourceProperties.getUser())
                .password(liquibaseDataSourceProperties.getPassword())
                .url(liquibaseDataSourceProperties.getUrl())
                .driverClassName(liquibaseDataSourceProperties.getDriver())
                .build();
        if (ds instanceof org.apache.tomcat.jdbc.pool.DataSource) {
            ((org.apache.tomcat.jdbc.pool.DataSource) ds).setInitialSize(1);
            ((org.apache.tomcat.jdbc.pool.DataSource) ds).setMaxActive(2);
            ((org.apache.tomcat.jdbc.pool.DataSource) ds).setMaxAge(1000);
            ((org.apache.tomcat.jdbc.pool.DataSource) ds).setMinIdle(0);
            ((org.apache.tomcat.jdbc.pool.DataSource) ds).setMinEvictableIdleTimeMillis(60000);
        } else {
            // warnings or exceptions, whatever you prefer
        }

        LOG.info("Initialized a datasource for {}", liquibaseDataSourceProperties.getUrl());
        return ds;
    }

}

可以在Tomcat的站点上找到属性的文档:https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html

The documentation of the properties can be found on the site of Tomcat: https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html

  • initialSize:启动池时创建的初始连接数
  • maxActive:可以同时从该池分配的最大活动连接数
  • minIdle:始终保持在池中的已建立连接的最小数量
  • maxAge:保持此连接的时间(以毫秒为单位).当连接返回到池中时,池将检查是否已达到现在-连接时> maxAge,如果达到,则关闭连接,而不是将其返回到池中.默认值为0,表示连接将保持打开状态,并且在将连接返回到池中时不会进行任何年龄检查.
  • minEvictableIdleTimeMillis:在有资格被驱逐之前,对象可能在池中空闲的最短时间.

因此,这似乎不是连接泄漏,它只是数据源的默认配置,如果您使用专用的数据源,则该配置对于Liquibase 不是最佳的选择.如果liquibase数据源是您的主要数据源,我不认为这会成为问题.

So it does not appear to be a connection leak, it's just the default configuration of the datasource which is not optimal for Liquibase if you use a dedicated datasource. I don't expect this to be a problem if the liquibase datasource is your primary datasource.

这篇关于Spring Boot应用程序中的Liquibase保持10个连接打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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