Skip to content

Commit

Permalink
Make username/password optional and add driver property
Browse files Browse the repository at this point in the history
  • Loading branch information
Chavjoh authored and radcortez committed Feb 1, 2024
1 parent 5420ff4 commit 7bb0db3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
13 changes: 11 additions & 2 deletions docs/modules/ROOT/pages/includes/quarkus-jdbc-config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ a| [[quarkus-jdbc-config_quarkus.config.source.jdbc.key]]`link:#quarkus-jdbc-con
--
Name of the column containing the key
--|string
|`key`
|`configuration_key`


a| [[quarkus-jdbc-config_quarkus.config.source.jdbc.value]]`link:#quarkus-jdbc-config_quarkus.config.source.jdbc.value[quarkus.config.source.jdbc.value]`
Expand All @@ -50,7 +50,7 @@ a| [[quarkus-jdbc-config_quarkus.config.source.jdbc.value]]`link:#quarkus-jdbc-c
--
Name of the column containing the value
--|string
|`value`
|`configuration_value`


a| [[quarkus-jdbc-config_quarkus.config.source.jdbc.username]]`link:#quarkus-jdbc-config_quarkus.config.source.jdbc.username[quarkus.config.source.jdbc.username]`
Expand Down Expand Up @@ -80,6 +80,15 @@ The datasource URL, if not defined the URL of the default datasource is used
| value of `quarkus.datasource..jdbc.url`


a| [[quarkus-jdbc-config_quarkus.config.source.jdbc.driver]]`link:#quarkus-jdbc-config_quarkus.config.source.jdbc.driver[quarkus.config.source.jdbc.driver]`

[.description]
--
The datasource driver, if not defined the driver of the default datasource is used
--|string
| value of `quarkus.datasource..jdbc.driver`


a| [[quarkus-jdbc-config_quarkus.config.source.jdbc.initial-size]]`link:#quarkus-jdbc-config_quarkus.config.source.jdbc.initial-size[quarkus.config.source.jdbc.initial-size]`

[.description]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkiverse.config.jdbc.runtime;

import java.time.Duration;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
Expand Down Expand Up @@ -57,20 +58,26 @@ public interface JdbcConfigConfig {
* The datasource username, if not defined the username of the default datasource is used
*/
@WithDefault("${quarkus.datasource.username}")
String username();
Optional<String> username();

/**
* The datasource password, if not defined the password of the default datasource is used
*/
@WithDefault("${quarkus.datasource.password}")
String password();
Optional<String> password();

/**
* The datasource URL, if not defined the URL of the default datasource is used
*/
@WithDefault("${quarkus.datasource.jdbc.url}")
String url();

/**
* The datasource driver, if not defined the driver of the default datasource is used
*/
@WithDefault("${quarkus.datasource.jdbc.driver}")
Optional<String> driver();

/**
* The initial size of the pool. Usually you will want to set the initial size to match at least the
* minimal size, but this is not enforced so to allow for architectures which prefer a lazy initialization
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkiverse.config.jdbc.runtime;

import static io.agroal.api.configuration.AgroalDataSourceConfiguration.DataSourceImplementation.AGROAL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -13,8 +15,6 @@
import org.jboss.logging.Logger;

import io.agroal.api.AgroalDataSource;
import io.agroal.api.configuration.supplier.AgroalConnectionFactoryConfigurationSupplier;
import io.agroal.api.configuration.supplier.AgroalConnectionPoolConfigurationSupplier;
import io.agroal.api.configuration.supplier.AgroalDataSourceConfigurationSupplier;
import io.agroal.api.security.NamePrincipal;
import io.agroal.api.security.SimplePassword;
Expand Down Expand Up @@ -82,29 +82,26 @@ public String getValue(String propertyName) {
}

private void prepareDataSource(final JdbcConfigConfig config) throws SQLException {
// create supplier
AgroalDataSourceConfigurationSupplier dataSourceConfiguration = new AgroalDataSourceConfigurationSupplier();
// get reference to connection pool
AgroalConnectionPoolConfigurationSupplier poolConfiguration = dataSourceConfiguration
.connectionPoolConfiguration();
// get reference to connection factory
AgroalConnectionFactoryConfigurationSupplier connectionFactoryConfiguration = poolConfiguration
.connectionFactoryConfiguration();

// configure pool
poolConfiguration
.initialSize(config.initialSize())
.minSize(config.minSize())
.maxSize(config.maxSize())
.acquisitionTimeout(config.acquisitionTimeout());

// configure supplier
connectionFactoryConfiguration
.jdbcUrl(config.url())
.credential(new NamePrincipal(config.username()))
.credential(new SimplePassword(config.password()));

dataSource = AgroalDataSource.from(dataSourceConfiguration.get());
var configuration = new AgroalDataSourceConfigurationSupplier()
.dataSourceImplementation(AGROAL)
.metricsEnabled(false)
.connectionPoolConfiguration(cp -> cp
.minSize(config.minSize())
.maxSize(config.maxSize())
.initialSize(config.initialSize())
.acquisitionTimeout(config.acquisitionTimeout())
.connectionFactoryConfiguration(cf -> cf
.jdbcUrl(config.url())
.connectionProviderClassName(config.driver()
.orElse(null))
.principal(config.username()
.map(NamePrincipal::new)
.orElse(null))
.credential(config.password()
.map(SimplePassword::new)
.orElse(null))));

dataSource = AgroalDataSource.from(configuration);
}

private void prepareQueries(final JdbcConfigConfig config) {
Expand Down

0 comments on commit 7bb0db3

Please sign in to comment.