-
Notifications
You must be signed in to change notification settings - Fork 41.2k
BeanCurrentlyInCreationException when two DataSources used with DataSourceInitializer #9394
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
Comments
We intend to do that, yes see #6560. There could be a way to break that cycle though. |
Thanks and sorry I missed that issue in my search. I see you've asked to keep convo here and will do so. I do agree that I would like to resolve this without requiring that approach. Why does |
Ironically enough, that test passes in IntelliJ IDEA. I can see where the problem comes from now. The BPP attempts to fully initialize I am not sure I get why IJ does not reveal the cycle. But I think I have an idea to fix it. |
@snicoll thanks, that was my analysis as well. I did find a workaround - by using |
Given you have a workaround and changing things in the BPP may break other use cases, I am tempted to close this issue as a duplicate of #9528. Let see what the rest of the team think. |
Yes, I think a DUP of #9528 makes sense. Thanks for looking and opening that issue. |
Duplicate of #9528 |
@zachmarshall @snicoll it seems that your workaround does not work in Spring Boot 2.0 app with two secondary data sources and one primary RoutingDataSource. This is my config:
ReplicationRoutingDataSource:
What I get is:
When I tried to exclude some auto configurations and commented out
Any thoughts how to fix it? Spring Boot 2.0.0.RELEASE |
@rvit34 Can probably wrap the DataSource class DSHolder {
DataSource dataSource
}
|
I don't have such bean I am getting this issue after upgrading to Spring Boot 2.1.3 from 1.5.X
@rvit34 Did you find a solution? |
@AleksandarTokarev I cleaned my code above and forget about solution. But I remember that it worked. Now I am using similar configuration with SB 2.1/2.2 and Kotlin. And It works:
|
@AleksandarTokarev The easiest solution is to exclude DataSourceAutoConfiguration.class |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
If the version you are using still has this issues and u use mybatis-starter,And need to configure multiple data source beans in Beanfactory
|
It appears that when there are two
DataSource
beans created and one bean factory method calls the otherDataSource
's bean factory method (e.g. in a delegate pattern),DataSourceInitializer
triggers a circular bean reference. It doesn't help that one bean is tagged@Primary
.Apologies if I'm doing something incorrectly. I posted at Stack Overflow and did not get responses. I reviewed the doc and I believe I've followed that, and it does not mention this delegate pattern.
I've created a sample repository with two junit tests that demonstrate the issue. The full stack trace is also available there along with some analysis. In summary, the
@Primary DataSource
bean triggers creation of the subordinateDataSource
bean which completes successfully. The creation of the subordinate bean triggersDataSourceInitializer
which asks the bean factory for theDataSource
and the bean factory selects the@Primary
which is still in creation.I've reviewed both open and closed issues and found several relevant issues: #2383 is similar but isn't using
@Primary
, #8068 seems unrelated, #7652 mentions tricks needed for a similar setup but doesn't have the same relationship between the two beans, #5541 implies this should work but again doesn't have one factory bean calling the other factory bean, #5104 suggests using@Primary
which doesn't help in this case. #2784 seems to be the closest match and was closed per 9c73312. However, 9c73312 only seems to update the Rabbit and JMS configuration. Should@ConditionalOnSingleCandidate
also be used for theDataSourceInitializer
?The text was updated successfully, but these errors were encountered: