From 726736aeec81c57d0f536e41e5518cc8338e22ed Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Fri, 9 Oct 2020 15:45:42 -0300 Subject: [PATCH] Support additional JDBC properties for a DataSource Some environments use these properties for additional metadata (eg. in Google Cloud SQL) Closes #6634 Co-authored-by: Mihai B --- .../agroal/test/DefaultDataSourceConfigTest.java | 13 +++++++++---- .../application-default-datasource.properties | 4 +++- .../agroal/runtime/DataSourceJdbcRuntimeConfig.java | 8 ++++++++ .../java/io/quarkus/agroal/runtime/DataSources.java | 6 ++++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/extensions/agroal/deployment/src/test/java/io/quarkus/agroal/test/DefaultDataSourceConfigTest.java b/extensions/agroal/deployment/src/test/java/io/quarkus/agroal/test/DefaultDataSourceConfigTest.java index ad3495062885c..6783d51864248 100644 --- a/extensions/agroal/deployment/src/test/java/io/quarkus/agroal/test/DefaultDataSourceConfigTest.java +++ b/extensions/agroal/deployment/src/test/java/io/quarkus/agroal/test/DefaultDataSourceConfigTest.java @@ -1,9 +1,11 @@ package io.quarkus.agroal.test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.entry; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.sql.Connection; import java.sql.SQLException; import java.time.Duration; @@ -38,7 +40,7 @@ public void testDefaultDataSourceInjection() throws SQLException { private static void testDataSource(AgroalDataSource dataSource, String username, int minSize, int maxSize, int initialSize, Duration backgroundValidationInterval, Duration acquisitionTimeout, Duration leakDetectionInterval, - Duration idleRemovalInterval, Duration maxLifetime, String newConnectionSql) throws SQLException { + Duration idleRemovalInterval, Duration maxLifetime, String newConnectionSql) { AgroalConnectionPoolConfiguration configuration = dataSource.getConfiguration().connectionPoolConfiguration(); AgroalConnectionFactoryConfiguration agroalConnectionFactoryConfiguration = configuration .connectionFactoryConfiguration(); @@ -59,7 +61,10 @@ private static void testDataSource(AgroalDataSource dataSource, String username, assertTrue(agroalConnectionFactoryConfiguration.trackJdbcResources()); assertTrue(dataSource.getConfiguration().metricsEnabled()); assertEquals(newConnectionSql, agroalConnectionFactoryConfiguration.initialSql()); - try (Connection connection = dataSource.getConnection()) { - } + assertThat(agroalConnectionFactoryConfiguration.jdbcProperties()) + .contains( + entry("extraProperty1", "extraProperty1Value"), + entry("extraProperty2", "extraProperty2Value")); + assertThatCode(() -> dataSource.getConnection().close()).doesNotThrowAnyException(); } } diff --git a/extensions/agroal/deployment/src/test/resources/application-default-datasource.properties b/extensions/agroal/deployment/src/test/resources/application-default-datasource.properties index 2ed141440c361..83ff238367387 100644 --- a/extensions/agroal/deployment/src/test/resources/application-default-datasource.properties +++ b/extensions/agroal/deployment/src/test/resources/application-default-datasource.properties @@ -12,4 +12,6 @@ quarkus.datasource.jdbc.leak-detection-interval=55 quarkus.datasource.jdbc.idle-removal-interval=56 quarkus.datasource.jdbc.max-lifetime=57 quarkus.datasource.jdbc.transaction-isolation-level=serializable -quarkus.datasource.jdbc.new-connection-sql=create schema if not exists schema_default \ No newline at end of file +quarkus.datasource.jdbc.new-connection-sql=create schema if not exists schema_default +quarkus.datasource.jdbc.additional-jdbc-properties.extraProperty1=extraProperty1Value +quarkus.datasource.jdbc.additional-jdbc-properties.extraProperty2=extraProperty2Value \ No newline at end of file diff --git a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSourceJdbcRuntimeConfig.java b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSourceJdbcRuntimeConfig.java index 5b9c310b44676..34c7556b662c5 100644 --- a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSourceJdbcRuntimeConfig.java +++ b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSourceJdbcRuntimeConfig.java @@ -1,6 +1,7 @@ package io.quarkus.agroal.runtime; import java.time.Duration; +import java.util.Map; import java.util.Optional; import java.util.OptionalInt; @@ -103,4 +104,11 @@ public class DataSourceJdbcRuntimeConfig { */ @ConfigItem(defaultValue = "true") public boolean poolingEnabled = true; + + /** + * Other unspecified properties to be passed to the JDBC driver when creating new connections. + */ + @ConfigItem + public Map additionalJdbcProperties; + } diff --git a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java index f2f6a6435378a..b5d8973b95c1d 100644 --- a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java +++ b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java @@ -5,6 +5,7 @@ import java.sql.Statement; import java.util.Collection; import java.util.Iterator; +import java.util.Map; import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -250,6 +251,11 @@ private void applyNewConfiguration(AgroalDataSourceConfigurationSupplier dataSou .credential(new AgroalVaultCredentialsProviderPassword(name, credentialsProvider)); } + // Extra JDBC properties + for (Map.Entry entry : dataSourceJdbcRuntimeConfig.additionalJdbcProperties.entrySet()) { + connectionFactoryConfiguration.jdbcProperty(entry.getKey(), entry.getValue()); + } + // Pool size configuration: poolConfiguration.minSize(dataSourceJdbcRuntimeConfig.minSize); poolConfiguration.maxSize(dataSourceJdbcRuntimeConfig.maxSize);