All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.quartz.runtime.QuarkusQuartzConnectionPoolProvider Maven / Gradle / Ivy

There is a newer version: 3.17.0
Show newest version
package io.quarkus.quartz.runtime;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.quartz.utils.PoolingConnectionProvider;

import io.agroal.api.AgroalDataSource;
import io.quarkus.agroal.DataSource.DataSourceLiteral;
import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;

public class QuarkusQuartzConnectionPoolProvider implements PoolingConnectionProvider {
    private AgroalDataSource dataSource;
    private static String dataSourceName;

    public QuarkusQuartzConnectionPoolProvider() {
        final ArcContainer container = Arc.container();
        final InstanceHandle instanceHandle;
        final boolean useDefaultDataSource = "QUARKUS_QUARTZ_DEFAULT_DATASOURCE".equals(dataSourceName);
        if (useDefaultDataSource) {
            instanceHandle = container.instance(AgroalDataSource.class);
        } else {
            instanceHandle = container.instance(AgroalDataSource.class, new DataSourceLiteral(dataSourceName));
        }
        if (instanceHandle.isAvailable()) {
            this.dataSource = instanceHandle.get();
        } else {
            String message = String.format(
                    "JDBC Store configured but '%s' datasource is missing. You can configure your datasource by following the guide available at: https://quarkus.io/guides/datasource",
                    useDefaultDataSource ? "default" : dataSourceName);
            throw new IllegalStateException(message);
        }
    }

    @Override
    public DataSource getDataSource() {
        return dataSource;
    }

    @Override
    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    @Override
    public void shutdown() {
        // Do nothing as the connection will be closed inside the Agroal extension
    }

    @Override
    public void initialize() {

    }

    static void setDataSourceName(String dataSourceName) {
        QuarkusQuartzConnectionPoolProvider.dataSourceName = dataSourceName;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy