diff --git a/extensions/reactive-db2-client/runtime/src/main/java/io/quarkus/reactive/db2/client/runtime/DB2PoolRecorder.java b/extensions/reactive-db2-client/runtime/src/main/java/io/quarkus/reactive/db2/client/runtime/DB2PoolRecorder.java index 8ac5b757336e84..c948454aa75afe 100644 --- a/extensions/reactive-db2-client/runtime/src/main/java/io/quarkus/reactive/db2/client/runtime/DB2PoolRecorder.java +++ b/extensions/reactive-db2-client/runtime/src/main/java/io/quarkus/reactive/db2/client/runtime/DB2PoolRecorder.java @@ -9,12 +9,11 @@ import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions; import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions; +import java.util.Collections; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; -import jakarta.enterprise.inject.Instance; - import org.jboss.logging.Logger; import io.quarkus.arc.Arc; @@ -26,14 +25,21 @@ import io.quarkus.reactive.datasource.ReactiveDataSource; import io.quarkus.reactive.datasource.runtime.DataSourceReactiveRuntimeConfig; import io.quarkus.reactive.datasource.runtime.DataSourcesReactiveRuntimeConfig; +import io.quarkus.reactive.datasource.runtime.PoolCloseFutureFactory; +import io.quarkus.reactive.datasource.runtime.ReactiveDatasourceConnectionProvider; +import io.quarkus.reactive.datasource.runtime.ReactiveDatasourceCredentialsProvider; import io.quarkus.reactive.db2.client.DB2PoolCreator; import io.quarkus.runtime.RuntimeValue; import io.quarkus.runtime.ShutdownContext; import io.quarkus.runtime.annotations.Recorder; import io.vertx.core.Vertx; +import io.vertx.core.impl.CloseFuture; +import io.vertx.core.impl.VertxInternal; import io.vertx.db2client.DB2ConnectOptions; import io.vertx.db2client.DB2Pool; +import io.vertx.db2client.spi.DB2Driver; import io.vertx.sqlclient.PoolOptions; +import jakarta.enterprise.inject.Instance; @Recorder public class DB2PoolRecorder { @@ -48,7 +54,7 @@ public RuntimeValue configureDB2Pool(RuntimeValue vertx, DataSourcesReactiveDB2Config dataSourcesReactiveDB2Config, ShutdownContext shutdown) { - DB2Pool db2Pool = initialize(vertx.getValue(), + DB2Pool db2Pool = initialize((VertxInternal) vertx.getValue(), eventLoopCount.get(), dataSourceName, dataSourcesRuntimeConfig.getDataSourceRuntimeConfig(dataSourceName), @@ -63,7 +69,7 @@ public RuntimeValue mutinyDB2Pool(RuntimeValu return new RuntimeValue<>(io.vertx.mutiny.db2client.DB2Pool.newInstance(db2Pool.getValue())); } - private DB2Pool initialize(Vertx vertx, + private DB2Pool initialize(VertxInternal vertx, Integer eventLoopCount, String dataSourceName, DataSourceRuntimeConfig dataSourceRuntimeConfig, @@ -73,14 +79,46 @@ private DB2Pool initialize(Vertx vertx, dataSourceReactiveDB2Config); DB2ConnectOptions connectOptions = toConnectOptions(dataSourceRuntimeConfig, dataSourceReactiveRuntimeConfig, dataSourceReactiveDB2Config); - - // Use the convention defined by Quarkus Micrometer Vert.x metrics to create metrics prefixed with db2. and + CloseFuture poolCloseFuture = PoolCloseFutureFactory.create(vertx); + ReactiveDatasourceConnectionProvider connectionProvider = toConnectionProvider(vertx, + connectOptions, dataSourceRuntimeConfig, poolCloseFuture); // Use the convention defined by Quarkus Micrometer Vert.x metrics to create metrics prefixed with db2. and // the client_name as tag. // See io.quarkus.micrometer.runtime.binder.vertx.VertxMeterBinderAdapter.extractPrefix and // io.quarkus.micrometer.runtime.binder.vertx.VertxMeterBinderAdapter.extractClientName connectOptions.setMetricsName("db2|" + dataSourceName); - return createPool(vertx, poolOptions, connectOptions, dataSourceName); + return createPool(vertx, poolOptions, connectOptions, dataSourceName, connectionProvider, poolCloseFuture); + } + + private ReactiveDatasourceConnectionProvider toConnectionProvider(VertxInternal vertx, + DB2ConnectOptions connectOptions, DataSourceRuntimeConfig dataSourceRuntimeConfig, CloseFuture poolCloseFuture) { + ReactiveDatasourceCredentialsProvider reactiveDatasourceCredentialsProvider; + if (dataSourceRuntimeConfig.credentialsProvider.isPresent()) { + String beanName = dataSourceRuntimeConfig.credentialsProviderName.orElse(null); + CredentialsProvider credentialsProvider = CredentialsProviderFinder.find(beanName); + String name = dataSourceRuntimeConfig.credentialsProvider.get(); + Map credentials = credentialsProvider.getCredentials(name); + String user = credentials.get(USER_PROPERTY_NAME); + String password = credentials.get(PASSWORD_PROPERTY_NAME); + if (user != null) { + connectOptions.setUser(user); + } + if (password != null) { + connectOptions.setPassword(password); + } + reactiveDatasourceCredentialsProvider = new ReactiveDatasourceCredentialsProvider(credentialsProvider, name); + } else { + reactiveDatasourceCredentialsProvider = new ReactiveDatasourceCredentialsProvider(new CredentialsProvider() { + @Override + public Map getCredentials(String credentialsProviderName) { + return Map.of(USER_PROPERTY_NAME, connectOptions.getUser(), PASSWORD_PROPERTY_NAME, + connectOptions.getPassword()); + } + }, null); + } + + return new ReactiveDatasourceConnectionProvider<>(vertx, DB2Driver.INSTANCE, reactiveDatasourceCredentialsProvider, + connectOptions, DB2ConnectOptions::new, poolCloseFuture); } private PoolOptions toPoolOptions(Integer eventLoopCount, @@ -137,22 +175,6 @@ private DB2ConnectOptions toConnectOptions(DataSourceRuntimeConfig dataSourceRun connectOptions.setPassword(dataSourceRuntimeConfig.password.get()); } - // credentials provider - if (dataSourceRuntimeConfig.credentialsProvider.isPresent()) { - String beanName = dataSourceRuntimeConfig.credentialsProviderName.orElse(null); - CredentialsProvider credentialsProvider = CredentialsProviderFinder.find(beanName); - String name = dataSourceRuntimeConfig.credentialsProvider.get(); - Map credentials = credentialsProvider.getCredentials(name); - String user = credentials.get(USER_PROPERTY_NAME); - String password = credentials.get(PASSWORD_PROPERTY_NAME); - if (user != null) { - connectOptions.setUser(user); - } - if (password != null) { - connectOptions.setPassword(user); - } - } - connectOptions.setCachePreparedStatements(dataSourceReactiveRuntimeConfig.cachePreparedStatements); connectOptions.setSsl(dataSourceReactiveDB2Config.ssl); @@ -182,7 +204,8 @@ private DB2ConnectOptions toConnectOptions(DataSourceRuntimeConfig dataSourceRun } private DB2Pool createPool(Vertx vertx, PoolOptions poolOptions, DB2ConnectOptions dB2ConnectOptions, - String dataSourceName) { + String dataSourceName, ReactiveDatasourceConnectionProvider connectionProvider, + CloseFuture poolCloseFuture) { Instance instance; if (DataSourceUtil.isDefault(dataSourceName)) { instance = Arc.container().select(DB2PoolCreator.class); @@ -194,7 +217,10 @@ private DB2Pool createPool(Vertx vertx, PoolOptions poolOptions, DB2ConnectOptio DB2PoolCreator.Input input = new DefaultInput(vertx, poolOptions, dB2ConnectOptions); return instance.get().create(input); } - return DB2Pool.pool(vertx, dB2ConnectOptions, poolOptions); + DB2Pool pool = DB2Driver.INSTANCE + .newPool(vertx, Collections.singletonList(dB2ConnectOptions), poolOptions, poolCloseFuture) + .connectionProvider(connectionProvider); + return pool; } private static class DefaultInput implements DB2PoolCreator.Input {