[英]Spring Boot application.yml set datasource property globally, being ignored
I have a Spring Boot 1.5 app which is configured with an application.yml
file.我有一个配置了
application.yml
文件的 Spring Boot 1.5 应用application.yml
。 I need to manage the connection pool which is default - Tomcat.我需要管理默认的连接池 - Tomcat。
The problem is that the application.yml
has a datasources
property for several datasources.问题是
application.yml
有多个数据源的datasources
属性。 My global datasource.max-active=10
(UPDATE: datasource.tomcat.max-active=10
is ignored too) is being completely ignored (I created a test to see what datasource is being injected, and default maxActive
in them is set to 100).我的全局
datasource.max-active=10
(更新: datasource.tomcat.max-active=10
也被忽略)被完全忽略(我创建了一个测试来查看正在注入的数据源,并且它们中的默认maxActive
设置为100)。 I have to add it to every datasource separately to make the pool work the way I need it to.我必须将它分别添加到每个数据源,以使池按照我需要的方式工作。
The application.yml
looks like this (this is only part of it), and it creates datasource with maxActive=10
, but there is bunch of repetitions: application.yml
看起来像这样(这只是其中的一部分),它使用maxActive=10
创建数据源,但是有一堆重复:
spring:
.... #bunch of stuff, deleted for simplicity
datasource: #Added by me, ignored by Spring
max-active: 10 #Added by me, ignored by Spring
datasources:
datasource1:
url: jdbc:mysql://url:port1
max-active: 10 #Added by me, works
datasource2:
url: jdbc:mysql://url:port2
max-active: 10 #Added by me, works
Question: what is the correct way to set the max-active
property globally to avoid this repetition?问题:全局设置
max-active
属性以避免这种重复的正确方法是什么? Thanks.谢谢。
When you are using multiple data sources, spring boot does not provide a default data source auto-configuration.当您使用多个数据源时,spring boot 不提供默认的数据源自动配置。 This also means you have to provide connection pool properties for each data source.
这也意味着您必须为每个数据源提供连接池属性。 I don't think it's possible to set it globally.
我认为不可能在全球范围内设置它。 Instead, you can use the placeholder to set it once and use it everywhere.
相反,您可以使用占位符设置一次并在任何地方使用它。
custom:
max-active: 10
spring:
datasources:
datasource1:
url: jdbc:mysql://url:port1
max-active: ${custom.max-active}
datasource2:
url: jdbc:mysql://url:port2
max-active: ${custom.max-active}
You can find more info here: How to setup multiple connection pools when multiple datasources are used in Spring Boot?您可以在此处找到更多信息: 在 Spring Boot 中使用多个数据源时如何设置多个连接池?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.