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

Allow auto-configured org.jooq.Configuration to be used to create a custom DSLContext #42400

Closed
davinkevin opened this issue Sep 20, 2024 · 2 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@davinkevin
Copy link
Contributor

Hello 👋

I face a case where I want to "enhance" the DSLContext provided by JOOQ, to leverage MetricsDSLContext features of Micrometer.
However, such "decorator" is complicated to configure in Spring Boot, due to the restriction on depdendency cycles.

I wanted to do something like this, but it fails:

    @Bean
    fun dslContext(configuration: org.jooq.Configuration, registry: MeterRegistry): MetricsDSLContext {
        return MetricsDSLContext.withMetrics(configuration.dsl(), registry, Tags.of("query", "jooq") )
    }

And it's not possible to only inject the org.jooq.Configuration bean, because it's created only if there is no bean of type DSLContext defined… and I define one 🤷

I had to do that in my code to have something ok:

@Configuration
@EnableConfigurationProperties(JooqProperties::class)
class JooqConfig {

    @Bean
    fun jooqConfiguration(
        properties: JooqProperties,
        connectionProvider: ConnectionProvider?,
        dataSource: DataSource?,
        transactionProvider: ObjectProvider<TransactionProvider?>,
        executeListenerProviders: ObjectProvider<ExecuteListenerProvider?>,
        configurationCustomizers: ObjectProvider<DefaultConfigurationCustomizer>,
    ): DefaultConfiguration {
        return DefaultConfiguration().apply {
            set(properties.determineSqlDialect(dataSource))
            set(connectionProvider)
            transactionProvider.ifAvailable(this::set)
            configurationCustomizers.orderedStream().forEach { it.customize(this) }
            set(*executeListenerProviders.orderedStream().collect(Collectors.toList()).toTypedArray())
        }
    }

    @Bean
    fun dslContext(configuration: org.jooq.Configuration, registry: MeterRegistry): MetricsDSLContext {
        return MetricsDSLContext.withMetrics(configuration.dsl(), registry, Tags.of("query", "jooq") )
    }
}

The @EnableConfigurationProperties(JooqProperties::class) and fun jooqConfiguration comes from the Spring code base #copypaste

This limitation is the result of the DslContextConfiguration class, it hosts both the DSLContext and Configuration beans.

Would it be possible to split it to allow users to use the predefined Configuration generated by Spring Boot without the DSLContext?

If it's ok for you, I'll be glad to open a PR, but I wanted to present the case before doing an implementation.

Thank you!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 20, 2024
@wilkinsona wilkinsona changed the title feat(jooq): provide the org.jooq.Configuration for alternative DSLContext usage Provide the org.jooq.Configuration for alternative DSLContext usage Sep 20, 2024
@wilkinsona
Copy link
Member

This sort of decoration of a bean is typically a good use case for a bean post-processor. You could try something like this:

@Bean
static BeanPostProcessor jooqMetrics(ObjectProvider<MeterRegistry> meterRegistry) {
	return new BeanPostProcessor() {

		@Override
		public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
			if (bean instanceof DSLContext dslContext) {
				return new MetricsDSLContext(dslContext, meterRegistry.getObject(), Tags.of("query", "jooq"));
			}
			return bean;
		}

	};
}

@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 20, 2024
@snicoll snicoll added the status: waiting-for-triage An issue we've not yet triaged label Sep 20, 2024
@davinkevin
Copy link
Contributor Author

I agree, but I reported this issue because I had the same problem before introduction of r2dbc in spring-boot using jooq. In that context, the bean postprocessor was not a possible solution.

So, would you like a PR to split org.jooq.Configuration and DSLContext auto configuration, so we leverage the first even if the second is under configuration?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Sep 21, 2024
@wilkinsona wilkinsona changed the title Provide the org.jooq.Configuration for alternative DSLContext usage Allow auto-configured org.jooq.Configuration to be used to create a custom DSLContext Sep 23, 2024
@wilkinsona wilkinsona added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Sep 23, 2024
@wilkinsona wilkinsona added this to the 3.4.x milestone Sep 23, 2024
@wilkinsona wilkinsona self-assigned this Sep 23, 2024
@wilkinsona wilkinsona modified the milestones: 3.4.x, 3.4.0-RC1 Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants