在Spring Boot中使用多个数据源时,如何设置多个连接池? [英] How to setup multiple connection pools when multiple datasources are used in Spring Boot?

查看:626
本文介绍了在Spring Boot中使用多个数据源时,如何设置多个连接池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Boot应用程序,该应用程序连接到两个单独的数据库.一切正常(我遵循了文档教程),尽管为了自定义Tomcat JDBC连接池设置,我必须手动配置(因为通过定义多个数据源,Boot的自动配置将被忽略,Spring Boot不再从application.properties中读取特定于Tomcat的属性).

I have a Spring Boot application that connects to two separate databases. All works fine (I followed the steps in the docs and a tutorial), although in order to customize the Tomcat JDBC connection pool settings, I had to manually configure it (because by defining multiple data sources, the Boot auto-configuration is ignored, and Spring Boot does not read the tomcat-specific properties anymore from application.properties).

在配置两个数据源期间使用调试器时,我看到两个

When I use a debugger during the configuration of the two DataSources, I see that both org.apache.tomcat.jdbc.pool.DataSource instances have the same connection pool in the DataSource.PoolProperties["name"] entry. See below screenshots in the debugger, each dataSource() method is configured in a separate configuration class. Notice that the same Connection Pool is defined.

但是,从我使用jConsole + tomcat JMX看到的结果来看,只有一个连接池,其中配置了主要数据库详细信息(URL,凭据,请参见下文).

However, from what I see using jConsole + tomcat JMX, there is only one connection pool, which has the primary database details configured (URL, credentials, see below).

由于Spring内部有多个抽象层,因此我很难调试它.我有 Eclipse Class Decompiler插件,我通常用它来查看Spring逻辑,但是在这种情况下,数据源的初始化代码在注册bean时发生,而不是在Spring Boot实际用于设置数据源时发生.

Because of the multiple layers of abstraction inside Spring, it is difficult for me to debug this. I have the Eclipse Class Decompiler plugin, which I normally use to see the Spring logic, but in this case, the initialization code for the data sources happens when the beans are registered, not when they are actually used by Spring Boot to set the data sources up.

最重要的是,您能帮我理解吗?

Bottom line, can you help me understand:

  1. 为什么只有一个连接池
  2. 如何使用两个连接池,每个数据源一个
  3. 在Spring代码中的何处查看有关其工作原理的更多详细信息

对于第二个问题,有一个相关问题,但没有答案.还有另一个问题,它是误报,并且<一个href ="https://stackoverflow.com/questions/29330592/sp​​ring-initialize-multiple-connection-pools-at-startup">另一个与Spring相关的,而不是Spring Boot,所以请不要不会将其举报为欺诈.

For the 2nd question, there is a somewhat related question, but with no answer. There is another question which is a false positive, and another one which is related to Spring, not Spring Boot, so please don't report this as dupe.

推荐答案

我正在回答当时的所作所为.如果您找到更好的解决方案,或者Spring允许多个连接池,请发布答案,我会选择您的答案.

由于给定我在问题中发布的代码,Spring只会配置一个连接池(在validationQuery 和 validationInterval )://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html#Common_Attributes"rel =" nofollow noreferrer> tomcat CP ),我添加了一个计划方法来使第二个数据源保持活动状态.

Because Spring will configure, given the code I posted in the question, only one connection pool (setting the validationQuery and validationInterval on the tomcat CP), I added a scheduled method to keep alive my second data source.

@Scheduled(fixedRate=INTERVAL_IN_MS)
public void scheduledTestDatabaseConnection() {
    try {
        testDatabaseConnection();
        LOGGER.trace("Tested EJBCA DB connection with success");
    }
    catch (Exception e) {
        LOGGER.error("Got an error when refreshing the EJBCA DB connection '{}'", e.getMessage());
    }
}

在上面的示例中,testDatabaseConnection()在Spring Data Repository上调用了一个方法

In the above example, testDatabaseConnection() calls a method on the Spring Data Repository

@Query("SELECT 1 FROM MyTable")
public int testConnection();

这篇关于在Spring Boot中使用多个数据源时,如何设置多个连接池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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