io.quarkus.agroal.DataSource Java Examples

The following examples show how to use io.quarkus.agroal.DataSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DevModeTestEndpoint.java    From quarkus with Apache License 2.0 7 votes vote down vote up
@GET
@Path("/{dataSourceName}/{jdbcUrl}/{username}/{minSize}/{maxSize}")
public String test(@PathParam("dataSourceName") String dataSourceName, @PathParam("jdbcUrl") String jdbcUrl,
        @PathParam("username") String username,
        @PathParam("minSize") int minSize, @PathParam("maxSize") int maxSize) throws Exception {
    AgroalDataSource ds;
    if (dataSourceName.equals("default")) {
        ds = CDI.current().select(AgroalDataSource.class)
                .get();
    } else {
        ds = CDI.current().select(AgroalDataSource.class, new DataSource.DataSourceLiteral(dataSourceName))
                .get();
    }
    testDataSource(dataSourceName, ds, URLDecoder.decode(jdbcUrl, StandardCharsets.UTF_8.name()), username, minSize,
            maxSize);
    return "ok";
}
 
Example #2
Source File: AgroalProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static void validateBuildTimeConfig(AggregatedDataSourceBuildTimeConfigBuildItem aggregatedConfig) {
    DataSourceJdbcBuildTimeConfig jdbcBuildTimeConfig = aggregatedConfig.getJdbcConfig();

    String fullDataSourceName = aggregatedConfig.isDefault() ? "default datasource"
            : "datasource named '" + aggregatedConfig.getName() + "'";

    String driverName = aggregatedConfig.getResolvedDriverClass();
    Class<?> driver;
    try {
        driver = Class.forName(driverName, true, Thread.currentThread().getContextClassLoader());
    } catch (ClassNotFoundException e) {
        throw new ConfigurationException(
                "Unable to load the datasource driver " + driverName + " for the " + fullDataSourceName, e);
    }
    if (jdbcBuildTimeConfig.transactions == TransactionIntegration.XA) {
        if (!XADataSource.class.isAssignableFrom(driver)) {
            throw new ConfigurationException(
                    "Driver is not an XA dataSource, while XA has been enabled in the configuration of the "
                            + fullDataSourceName + ": either disable XA or switch the driver to an XADataSource");
        }
    } else {
        if (driver != null && !javax.sql.DataSource.class.isAssignableFrom(driver)
                && !Driver.class.isAssignableFrom(driver)) {
            if (aggregatedConfig.isDefault()) {
                throw new ConfigurationException(
                        "Driver " + driverName
                                + " is an XA datasource, but XA transactions have not been enabled on the default datasource; please either set 'quarkus.datasource.jdbc.transactions=xa' or switch to a standard non-XA JDBC driver implementation");
            } else {
                throw new ConfigurationException(
                        "Driver " + driverName
                                + " is an XA datasource, but XA transactions have not been enabled on the datasource named '"
                                + fullDataSourceName + "'; please either set 'quarkus.datasource." + fullDataSourceName
                                + ".jdbc.transactions=xa' or switch to a standard non-XA JDBC driver implementation");
            }
        }
    }
}
 
Example #3
Source File: AgroalProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Record(ExecutionTime.STATIC_INIT)
@BuildStep
void generateDataSourceSupportBean(AgroalRecorder recorder,
        List<AggregatedDataSourceBuildTimeConfigBuildItem> aggregatedBuildTimeConfigBuildItems,
        SslNativeConfigBuildItem sslNativeConfig,
        Capabilities capabilities,
        BuildProducer<AdditionalBeanBuildItem> additionalBeans,
        BuildProducer<SyntheticBeanBuildItem> syntheticBeanBuildItemBuildProducer) {
    if (aggregatedBuildTimeConfigBuildItems.isEmpty()) {
        // No datasource has been configured so bail out
        return;
    }

    // make a DataSourceProducer bean
    additionalBeans.produce(AdditionalBeanBuildItem.builder().addBeanClasses(DataSources.class).setUnremovable()
            .setDefaultScope(DotNames.SINGLETON).build());
    // add the @DataSource class otherwise it won't be registered as a qualifier
    additionalBeans.produce(AdditionalBeanBuildItem.builder().addBeanClass(DataSource.class).build());

    // add implementations of AgroalPoolInterceptor
    additionalBeans.produce(AdditionalBeanBuildItem.unremovableOf(AgroalPoolInterceptor.class));

    // create the DataSourceSupport bean that DataSourceProducer uses as a dependency
    DataSourceSupport dataSourceSupport = getDataSourceSupport(aggregatedBuildTimeConfigBuildItems, sslNativeConfig,
            capabilities);
    syntheticBeanBuildItemBuildProducer.produce(SyntheticBeanBuildItem.configure(DataSourceSupport.class)
            .supplier(recorder.dataSourceSupportSupplier(dataSourceSupport))
            .unremovable()
            .done());
}
 
Example #4
Source File: DataSourceTenantConnectionResolver.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Returns either the default data source or the tenant specific one.
 * 
 * @param tenantId Tenant identifier. The value is required (non-{@literal null}) in case of
 *        {@link MultiTenancyStrategy#DATABASE}.
 * @param strategy Current multitenancy strategy Required value that cannot be {@literal null}.
 * 
 * @return Data source.
 */
private static AgroalDataSource tenantDataSource(JPAConfig jpaConfig, String tenantId, MultiTenancyStrategy strategy) {
    if (strategy != MultiTenancyStrategy.SCHEMA) {
        return Arc.container().instance(AgroalDataSource.class, new DataSource.DataSourceLiteral(tenantId)).get();
    }
    String dataSourceName = jpaConfig.getMultiTenancySchemaDataSource();
    if (dataSourceName == null) {
        AgroalDataSource dataSource = Arc.container().instance(AgroalDataSource.class).get();
        return createFrom(dataSource.getConfiguration());
    }
    return Arc.container().instance(AgroalDataSource.class, new DataSource.DataSourceLiteral(dataSourceName)).get();
}
 
Example #5
Source File: AgroalProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
void generateDataSourceBeans(AgroalRecorder recorder,
        DataSourcesRuntimeConfig dataSourcesRuntimeConfig,
        List<AggregatedDataSourceBuildTimeConfigBuildItem> aggregatedBuildTimeConfigBuildItems,
        SslNativeConfigBuildItem sslNativeConfig,
        Capabilities capabilities,
        BuildProducer<SyntheticBeanBuildItem> syntheticBeanBuildItemBuildProducer,
        BuildProducer<JdbcDataSourceBuildItem> jdbcDataSource) {
    if (aggregatedBuildTimeConfigBuildItems.isEmpty()) {
        // No datasource has been configured so bail out
        return;
    }

    for (Map.Entry<String, DataSourceSupport.Entry> entry : getDataSourceSupport(aggregatedBuildTimeConfigBuildItems,
            sslNativeConfig,
            capabilities).entries.entrySet()) {

        String dataSourceName = entry.getKey();

        SyntheticBeanBuildItem.ExtendedBeanConfigurator configurator = SyntheticBeanBuildItem
                .configure(AgroalDataSource.class)
                .addType(DATA_SOURCE)
                .scope(Singleton.class)
                .setRuntimeInit()
                .unremovable()
                // pass the runtime config into the recorder to ensure that the DataSource related beans
                // are created after runtime configuration has been setup
                .supplier(recorder.agroalDataSourceSupplier(dataSourceName, dataSourcesRuntimeConfig));

        if (entry.getValue().isDefault) {
            configurator.addQualifier(Default.class);
        } else {
            // this definitely not ideal, but 'elytron-jdbc-security' uses it (although it could be easily changed)
            // which means that perhaps other extensions might depend on this as well...
            configurator.name(dataSourceName);

            configurator.addQualifier().annotation(DotNames.NAMED).addValue("value", dataSourceName).done();
            configurator.addQualifier().annotation(DataSource.class).addValue("value", dataSourceName).done();
        }

        syntheticBeanBuildItemBuildProducer.produce(configurator.done());

        jdbcDataSource.produce(new JdbcDataSourceBuildItem(dataSourceName,
                entry.getValue().resolvedDbKind,
                entry.getValue().isDefault));
    }
}
 
Example #6
Source File: DataSourceProducer.java    From database-rider with Apache License 2.0 4 votes vote down vote up
@Produces
@RiderPU("secondary")
public javax.sql.DataSource produceSecondary() {
    return secondaryDataSource;
}