-
Notifications
You must be signed in to change notification settings - Fork 48
fix(jooq): resolve DSLContext injection ambiguity #1911
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
Draft
graemerocher
wants to merge
4
commits into
7.0.x
Choose a base branch
from
fix-issue-1292-jooq-dslcontext
base: 7.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
46f0cd7
fix(jooq): resolve DSLContext injection ambiguity
graemerocher 527d0a9
fix(jooq): avoid multiple primary DSLContext beans for R2DBC each-bean
Copilot f5dbbeb
refactor(jooq): provide single primary r2dbc DSLContext alias
Copilot e650509
refactor(jooq): clarify primary r2dbc DSLContext selection
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,11 +15,19 @@ | |
| */ | ||
| package io.micronaut.configuration.jooq; | ||
|
|
||
| import io.micronaut.context.BeanProvider; | ||
| import io.micronaut.context.annotation.Any; | ||
| import io.micronaut.context.annotation.EachBean; | ||
| import io.micronaut.context.annotation.Factory; | ||
| import io.micronaut.context.annotation.Parameter; | ||
| import io.micronaut.context.annotation.Primary; | ||
| import io.micronaut.context.annotation.Requires; | ||
| import io.micronaut.context.annotation.Secondary; | ||
| import io.micronaut.core.annotation.Internal; | ||
| import io.micronaut.inject.qualifiers.Qualifiers; | ||
| import io.micronaut.transaction.TransactionStatus; | ||
| import io.micronaut.transaction.support.ExceptionUtil; | ||
| import jakarta.inject.Singleton; | ||
| import org.jooq.Configuration; | ||
| import org.jooq.DSLContext; | ||
| import org.jooq.TransactionProperty; | ||
|
|
@@ -43,8 +51,47 @@ final class DSLContextFactory { | |
| * @param configuration The {@link Configuration} | ||
| * @return A {@link DSLContext} | ||
| */ | ||
| @EachBean(Configuration.class) | ||
| DSLContext dslContext(Configuration configuration) { | ||
| @EachBean(JdbcConfiguration.class) | ||
| @Secondary | ||
| DSLContext dslContext(@Parameter JdbcConfiguration configuration) { | ||
| return createDslContext(configuration); | ||
| } | ||
|
|
||
| @EachBean(R2dbcConfiguration.class) | ||
| @Secondary | ||
| DSLContext r2dbcDslContext(@Parameter R2dbcConfiguration configuration) { | ||
| return createDslContext(configuration); | ||
| } | ||
|
|
||
| @Primary | ||
| @Singleton | ||
| @Requires(beans = R2dbcConfiguration.class) | ||
| DSLContext primaryR2dbcDslContext(@Any BeanProvider<R2dbcConfiguration> configurations) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the new primary alias duplicate one DSLContext? |
||
| R2dbcConfiguration configuration = selectR2dbcConfiguration(configurations); | ||
| return createDslContext(configuration); | ||
| } | ||
|
|
||
| private R2dbcConfiguration selectR2dbcConfiguration(BeanProvider<R2dbcConfiguration> configurations) { | ||
| return configurations.find(Qualifiers.byStereotype(Primary.class)) | ||
| .or(() -> configurations.find(Qualifiers.byName("default"))) | ||
| .orElseGet(() -> requireSingleConfiguration(configurations)); | ||
| } | ||
|
|
||
| private R2dbcConfiguration requireSingleConfiguration(BeanProvider<R2dbcConfiguration> configurations) { | ||
| var iterator = configurations.iterator(); | ||
| var configuration = iterator.next(); | ||
| if (iterator.hasNext()) { | ||
| int candidateCount = 2; | ||
| while (iterator.hasNext()) { | ||
| iterator.next(); | ||
| candidateCount++; | ||
| } | ||
| throw new IllegalStateException("Multiple R2DBC configurations found (" + candidateCount + "). Mark one @Primary or name one 'default'"); | ||
| } | ||
| return configuration; | ||
| } | ||
|
|
||
| private DSLContext createDslContext(Configuration configuration) { | ||
| return new DefaultDSLContext(configuration) { | ||
|
|
||
| @Override | ||
|
|
||
28 changes: 28 additions & 0 deletions
28
jooq/src/main/java/io/micronaut/configuration/jooq/JdbcConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * Copyright 2017-2026 original authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micronaut.configuration.jooq; | ||
|
|
||
| import io.micronaut.core.annotation.Internal; | ||
| import org.jooq.impl.DefaultConfiguration; | ||
|
|
||
| /** | ||
| * Internal JDBC-backed jOOQ configuration bean type. | ||
| * | ||
| * @since 7.0.0 | ||
| */ | ||
| @Internal | ||
| final class JdbcConfiguration extends DefaultConfiguration { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
jooq/src/main/java/io/micronaut/configuration/jooq/R2dbcConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * Copyright 2017-2026 original authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micronaut.configuration.jooq; | ||
|
|
||
| import io.micronaut.core.annotation.Internal; | ||
| import org.jooq.impl.DefaultConfiguration; | ||
|
|
||
| /** | ||
| * Internal R2DBC-backed jOOQ configuration bean type. | ||
| * | ||
| * @since 7.0.0 | ||
| */ | ||
| @Internal | ||
| final class R2dbcConfiguration extends DefaultConfiguration { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
jooq/src/test/groovy/io/micronaut/configuration/jooq/DslContextFactorySpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| * Copyright 2017-2022 original authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micronaut.configuration.jooq | ||
|
|
||
| import io.micronaut.context.ApplicationContext | ||
| import io.micronaut.context.annotation.Factory | ||
| import io.micronaut.context.annotation.Requires | ||
| import io.r2dbc.spi.ConnectionFactory | ||
| import jakarta.inject.Named | ||
| import jakarta.inject.Singleton | ||
| import org.jooq.DSLContext | ||
| import org.jooq.SQLDialect | ||
| import spock.lang.Specification | ||
|
|
||
| import javax.sql.DataSource | ||
| import java.lang.reflect.Proxy | ||
|
|
||
| class DslContextFactorySpec extends Specification { | ||
|
|
||
| void "test default dsl context can be injected with jdbc and r2dbc configurations"() { | ||
| given: | ||
| ApplicationContext applicationContext = ApplicationContext.run("multiple-config") | ||
|
|
||
| when: | ||
| TestService testService = applicationContext.getBean(TestService) | ||
|
|
||
| then: | ||
| testService.dslContext != null | ||
| testService.dslContext.configuration() instanceof R2dbcConfiguration | ||
|
|
||
| cleanup: | ||
| applicationContext.close() | ||
| } | ||
|
|
||
| @Singleton | ||
| @Requires(env = "multiple-config") | ||
| static class TestService { | ||
| final DSLContext dslContext | ||
|
|
||
| TestService(DSLContext dslContext) { | ||
| this.dslContext = dslContext | ||
| } | ||
| } | ||
|
|
||
| @Factory | ||
| @Requires(env = "multiple-config") | ||
| static class TestConnectionFactory { | ||
|
|
||
| @Singleton | ||
| @Named("default") | ||
| DataSource dataSource() { | ||
| return proxy(DataSource) | ||
| } | ||
|
|
||
| @Singleton | ||
| @Named("default") | ||
| ConnectionFactory connectionFactory() { | ||
| return proxy(ConnectionFactory) | ||
| } | ||
|
|
||
| @Singleton | ||
| @Named("analytics") | ||
| ConnectionFactory analyticsConnectionFactory() { | ||
| return proxy(ConnectionFactory) | ||
| } | ||
|
|
||
| @Singleton | ||
| @Named("default") | ||
| JooqConfigurationProperties jdbcJooqProperties() { | ||
| JooqConfigurationProperties configurationProperties = new JooqConfigurationProperties() | ||
| configurationProperties.sqlDialect = SQLDialect.H2 | ||
| return configurationProperties | ||
| } | ||
|
|
||
| @Singleton | ||
| @Named("default") | ||
| R2dbcJooqConfigurationProperties r2dbcJooqProperties() { | ||
| R2dbcJooqConfigurationProperties configurationProperties = new R2dbcJooqConfigurationProperties() | ||
| configurationProperties.sqlDialect = SQLDialect.POSTGRES | ||
| return configurationProperties | ||
| } | ||
|
|
||
| @Singleton | ||
| @Named("analytics") | ||
| R2dbcJooqConfigurationProperties analyticsR2dbcJooqProperties() { | ||
| R2dbcJooqConfigurationProperties configurationProperties = new R2dbcJooqConfigurationProperties() | ||
| configurationProperties.sqlDialect = SQLDialect.MYSQL | ||
| return configurationProperties | ||
| } | ||
|
|
||
| private static <T> T proxy(Class<T> type) { | ||
| return (T) Proxy.newProxyInstance(type.classLoader, [type] as Class<?>[]) { _, method, _ -> | ||
| if (method.name == "toString") { | ||
| return type.simpleName | ||
| } | ||
| if (method.returnType == Boolean.TYPE) { | ||
| return false | ||
| } | ||
| if (method.returnType == Integer.TYPE) { | ||
| return 0 | ||
| } | ||
| return null | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this cause regression for existing apps where users provide
Configurationbean, this might not work then since it will expect Jdbc/R2dbc/Configuration?This test would fail (passing in 7.0.x)