Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: data source definition overriding #5798

Merged

Conversation

dph5199278
Copy link
Contributor

修复由 #5671 导致无法设置其他默认数据源:

  1. 在多数据源或动态数据源下,无法设置其他数据源为默认dataSource

如下:

The bean 'dataSource', defined in class path resource [com/alibaba/druid/spring/boot3/autoconfigure/DruidDataSourceAutoConfigure.class], could not be registered. A bean with that name has already been defined in class path resource [....class] and overriding is disabled.
Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

如果开启spring.main.allow-bean-definition-overriding=true,会导致dataSource被DruidDataSourceAutoConfigure覆盖出现问题。

Signed-off-by: Dely <dph5199278@163.com>
@CLAassistant
Copy link

CLAassistant commented Mar 23, 2024

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@lizongbo
Copy link
Collaborator

没懂什么场景下需要这样操作,既然不想重复bean,不应该是把druid的starter依赖去掉么?
能详细描述下是什么样的配置在什么情况下触发问题么?

@dph5199278
Copy link
Contributor Author

有两个方面:

  1. 源码自身就有@ConditionalOnMissingBean,说明设计本身就是为了可替代性的
  2. 除了dataSource,其余都由autoconfig来创建,不用另行再做创建(即:只有dataSource本身,其余像WallFilter等还是交由starter统一配置)

@dph5199278
Copy link
Contributor Author

能详细描述下是什么样的配置在什么情况下触发问题么?

如:

@Configuration
public class DataSourceConfig {
    @Bean
    @Primary
    public DataSource dataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(new HashMap<>());
        return dynamicDataSource;
    }
}

如上,我需要一个动态源来切换具体的使用源,而TargetDataSources使用的就是value为druid的数据源,这样就会触发上面的问题,并且这个在1.2.21版本上是不会有问题的,DynamicDataSource是我这边自定义的动态数据源,除了这个,starter的其他配置都是统一全局的

@lizongbo
Copy link
Collaborator

ci构建失败, 请本地运行 mvn clean install验证ok

@dph5199278
Copy link
Contributor Author

dph5199278 commented Mar 27, 2024

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] druid-parent 1.2.23-SNAPSHOT ....................... SUCCESS [  1.213 s]
[INFO] druid 1.2.23-SNAPSHOT .............................. SUCCESS [03:11 min]
[INFO] druid-spring-boot-starter 1.2.23-SNAPSHOT .......... SUCCESS [ 25.349 s]
[INFO] druid-wrapper 1.2.23-SNAPSHOT ...................... SUCCESS [  0.214 s]
[INFO] druid-demo-petclinic 2.7.18 ........................ SUCCESS [ 22.051 s]
[INFO] druid-spring-boot-3-starter 1.2.23-SNAPSHOT ........ SUCCESS [ 20.881 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  04:21 min
[INFO] Finished at: 2024-03-27T19:43:46+08:00
[INFO] ------------------------------------------------------------------------

已和本地一致,本地可以编译

Signed-off-by: Dely <dph5199278@163.com>
@lizongbo lizongbo merged commit 920c53a into alibaba:master Mar 28, 2024
5 checks passed
@yuanyp
Copy link

yuanyp commented Jul 22, 2024

我这边发现从1.2.14升级到1.2.22发现一个问题,配置的最小连接数,初始化个数,最大连接数等都不生效,变成默认的了。升级到1.2.23又正常了。
spring.datasource.initial-size
spring.datasource.min-ide

配置类
@bean
@primary
@ConfigurationProperties(prefix="spring.datasource“)
public DataSource primaryDataSource(){
return DruidDataSourceBuilder.create().build();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants