Skip to content

Commit

Permalink
Support additional JDBC properties for a DataSource
Browse files Browse the repository at this point in the history
Some environments use these properties for additional metadata (eg. in Google Cloud SQL)

Closes quarkusio#6634

Co-authored-by: Mihai B <[email protected]>
  • Loading branch information
gastaldi and Mihai B committed Oct 12, 2020
1 parent e1b2375 commit 726736a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<String, String> additionalJdbcProperties;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -250,6 +251,11 @@ private void applyNewConfiguration(AgroalDataSourceConfigurationSupplier dataSou
.credential(new AgroalVaultCredentialsProviderPassword(name, credentialsProvider));
}

// Extra JDBC properties
for (Map.Entry<String, String> entry : dataSourceJdbcRuntimeConfig.additionalJdbcProperties.entrySet()) {
connectionFactoryConfiguration.jdbcProperty(entry.getKey(), entry.getValue());
}

// Pool size configuration:
poolConfiguration.minSize(dataSourceJdbcRuntimeConfig.minSize);
poolConfiguration.maxSize(dataSourceJdbcRuntimeConfig.maxSize);
Expand Down

0 comments on commit 726736a

Please sign in to comment.