Skip to content

Quartz select datasource at runtime #48545

@devjos

Description

@devjos

Describe the bug

My quarkus application has two datasources (postgres, mssql), but I activate only one at runtime.

I want quartz to use the active datasource as well. So I created a ConfigSourceInterceptorfor quarkus.quartz.datasource, so that quartz also uses the active datasource. However, the datasource seems to be determined at build time instead of runtime.

Configuration in application.properties

quarkus.datasource.postgresql.active=false
quarkus.datasource.postgresql.db-kind=postgresql
quarkus.datasource.postgresql.username=foo
quarkus.datasource.postgresql.jdbc.url=jdbc:postgresql://localhost:5432/test

quarkus.datasource.mssql.active=false
quarkus.datasource.mssql.db-kind=mssql
quarkus.datasource.mssql.username=foo
quarkus.datasource.mssql.jdbc.url=jdbc:sqlserver://localhost:1433;databaseName=test

quarkus.scheduler.start-mode=forced
quarkus.quartz.store-type=jdbc-cmt
quarkus.quartz.clustered=true
quarkus.quartz.serialize-job-data=true
@Priority(Priorities.APPLICATION + 200)
public class QuartzDatasourceInterceptor implements ConfigSourceInterceptor
{
	@Serial
	private static final long serialVersionUID = 367246512037404779L;

	private static final String QUARTZ_DATASOURCE_CONFIG_PROPERTY = "quarkus.quartz.datasource";

	@Override
	public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name)
	{
		if(QUARTZ_DATASOURCE_CONFIG_PROPERTY.equals(name))
		{
			if(isDatasourceActive(context, "postgresql"))
			{
				return new ConfigValue.ConfigValueBuilder()
					.withName(QUARTZ_DATASOURCE_CONFIG_PROPERTY)
					.withValue("postgresql")
					.build();
			}

			if(isDatasourceActive(context, "mssql"))
			{
				return new ConfigValue.ConfigValueBuilder()
					.withName(QUARTZ_DATASOURCE_CONFIG_PROPERTY)
					.withValue("mssql")
					.build();
			}

			ConfigLogging.log.fatal("Could not find active datasource");
			return null;
		}
		else
		{
			return doLocked(() -> context.proceed(name));
		}
	}

	private boolean isDatasourceActive(final ConfigSourceInterceptorContext context, String datasourceName)
	{
		ConfigValue configValue = doLocked(() -> context.restart("quarkus.datasource." + datasourceName + ".active"));
		boolean isActive = Boolean.parseBoolean(configValue.getValue());
		ConfigLogging.log.info(datasourceName + " active: " + isActive);
		return isActive;
	}
}

Expected behavior

There is a way to choose quarkus.quartz.datasource at runtime

Actual behavior

If i build with both datasources inactive I get Build step io.quarkus.quartz.deployment.QuartzProcessor#driver threw an exception: io.quarkus.runtime.configuration.ConfigurationException: JDBC Store configured but the 'default' datasource is not configured properly. You can configure your datasource by following the guide available at: https://quarkus.io/guides/datasource.

If I build with one datasource enabled, quarkus.quartz.datasource seems to be fixed to that datasource. If changed at runtime I get SQL exceptions such as Failure obtaining db row lock: ERROR: syntax error at or near "WITH"

How to Reproduce?

No response

Output of uname -a or ver

No response

Output of java -version

openjdk version "21.0.6" 2025-01-21 LTS
OpenJDK Runtime Environment Temurin-21.0.6+7 (build 21.0.6+7-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.6+7 (build 21.0.6+7-LTS, mixed mode, sharing)

Quarkus version or git rev

3.20.1

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /opt/homebrew/Cellar/maven/3.9.9/libexec
Java version: 21.0.6, vendor: Eclipse Adoptium, runtime: /Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "15.5", arch: "aarch64", family: "mac"

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions