From 9655a0886c2fba51f6b8bbf24364e42ba0821702 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 7 Mar 2024 11:42:43 +0200 Subject: [PATCH] Make mutiny version of pool use the already configured vertx pool Before this, the mutiny version would recreate the delegate instead of reusing the existing bean Fixes: #39047 (cherry picked from commit d894149879c0fbae83eb176cbb0e46765def7408) --- .../ReactiveDB2ClientProcessor.java | 12 ++++++--- .../db2/client/runtime/DB2PoolRecorder.java | 26 ++++++++++++++---- .../ReactiveMSSQLClientProcessor.java | 11 +++++--- .../mssql/client/SamePoolInstanceTest.java | 27 +++++++++++++++++++ .../client/runtime/MSSQLPoolRecorder.java | 27 +++++++++++++++---- .../ReactiveMySQLClientProcessor.java | 11 +++++--- .../mysql/client/SamePoolInstanceTest.java | 26 ++++++++++++++++++ .../client/runtime/MySQLPoolRecorder.java | 26 ++++++++++++++---- .../ReactiveOracleClientProcessor.java | 11 +++++--- .../oracle/client/SamePoolInstanceTest.java | 26 ++++++++++++++++++ .../client/runtime/OraclePoolRecorder.java | 26 ++++++++++++++---- .../deployment/ReactivePgClientProcessor.java | 12 ++++++--- .../pg/client/SamePoolInstanceTest.java | 27 +++++++++++++++++++ .../pg/client/runtime/PgPoolRecorder.java | 26 ++++++++++++++---- 14 files changed, 249 insertions(+), 45 deletions(-) create mode 100644 extensions/reactive-mssql-client/deployment/src/test/java/io/quarkus/reactive/mssql/client/SamePoolInstanceTest.java create mode 100644 extensions/reactive-mysql-client/deployment/src/test/java/io/quarkus/reactive/mysql/client/SamePoolInstanceTest.java create mode 100644 extensions/reactive-oracle-client/deployment/src/test/java/io/quarkus/reactive/oracle/client/SamePoolInstanceTest.java create mode 100644 extensions/reactive-pg-client/deployment/src/test/java/io/quarkus/reactive/pg/client/SamePoolInstanceTest.java diff --git a/extensions/reactive-db2-client/deployment/src/main/java/io/quarkus/reactive/db2/client/deployment/ReactiveDB2ClientProcessor.java b/extensions/reactive-db2-client/deployment/src/main/java/io/quarkus/reactive/db2/client/deployment/ReactiveDB2ClientProcessor.java index 4c94cc18e68c5..1c4a185b019ba 100644 --- a/extensions/reactive-db2-client/deployment/src/main/java/io/quarkus/reactive/db2/client/deployment/ReactiveDB2ClientProcessor.java +++ b/extensions/reactive-db2-client/deployment/src/main/java/io/quarkus/reactive/db2/client/deployment/ReactiveDB2ClientProcessor.java @@ -65,12 +65,16 @@ class ReactiveDB2ClientProcessor { - private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class), + private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create( + DotName.createSimple(Instance.class), new Type[] { ClassType.create(DotName.createSimple(DB2PoolCreator.class.getName())) }, null); private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0]; private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class); + private static final DotName VERTX_DB2_POOL = DotName.createSimple(DB2Pool.class); + private static final Type VERTX_DB2_POOL_TYPE = Type.create(VERTX_DB2_POOL, Type.Kind.CLASS); + @BuildStep @Record(ExecutionTime.RUNTIME_INIT) ServiceStartBuildItem build(BuildProducer feature, @@ -208,7 +212,7 @@ private void createPoolIfDefined(DB2PoolRecorder recorder, .defaultBean() .addType(Pool.class) .scope(ApplicationScoped.class) - .addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) + .addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) .addInjectionPoint(ClassType.create(DataSourceSupport.class)) .createWith(poolFunction) .unremovable() @@ -223,9 +227,9 @@ private void createPoolIfDefined(DB2PoolRecorder recorder, .defaultBean() .addType(io.vertx.mutiny.sqlclient.Pool.class) .scope(ApplicationScoped.class) - .addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) + .addInjectionPoint(VERTX_DB2_POOL_TYPE, injectionPointAnnotations(dataSourceName)) .addInjectionPoint(ClassType.create(DataSourceSupport.class)) - .createWith(recorder.mutinyDB2Pool(poolFunction)) + .createWith(recorder.mutinyDB2Pool(dataSourceName)) .unremovable() .setRuntimeInit(); 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 134ccf43fa381..b9fb3a361541d 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 @@ -10,11 +10,13 @@ import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions; import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions; +import java.lang.annotation.Annotation; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.function.Supplier; +import jakarta.enterprise.inject.Default; import jakarta.enterprise.inject.Instance; import jakarta.enterprise.util.TypeLiteral; @@ -47,7 +49,7 @@ public class DB2PoolRecorder { private static final Logger log = Logger.getLogger(DB2PoolRecorder.class); - private static final TypeLiteral> TYPE_LITERAL = new TypeLiteral<>() { + private static final TypeLiteral> POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() { }; public Function, DB2Pool> configureDB2Pool(RuntimeValue vertx, @@ -75,16 +77,30 @@ public DB2Pool apply(SyntheticCreationalContext context) { } public Function, io.vertx.mutiny.db2client.DB2Pool> mutinyDB2Pool( - Function, DB2Pool> function) { + String dataSourceName) { return new Function<>() { @SuppressWarnings("unchecked") @Override public io.vertx.mutiny.db2client.DB2Pool apply(SyntheticCreationalContext context) { - return io.vertx.mutiny.db2client.DB2Pool.newInstance(function.apply(context)); + DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class); + if (datasourceSupport.getInactiveNames().contains(dataSourceName)) { + throw DataSourceUtil.dataSourceInactive(dataSourceName); + } + DB2Pool db2Pool = (DB2Pool) context.getInjectedReference(DB2Pool.class, + getReactiveDataSourceQualifier(dataSourceName)); + return io.vertx.mutiny.db2client.DB2Pool.newInstance(db2Pool); } }; } + private static Annotation getReactiveDataSourceQualifier(String dataSourceName) { + if (DataSourceUtil.isDefault(dataSourceName)) { + return Default.Literal.INSTANCE; + } + + return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName); + } + private DB2Pool initialize(VertxInternal vertx, Integer eventLoopCount, String dataSourceName, @@ -240,9 +256,9 @@ private DB2Pool createPool(Vertx vertx, PoolOptions poolOptions, DB2ConnectOptio SyntheticCreationalContext context) { Instance instance; if (DataSourceUtil.isDefault(dataSourceName)) { - instance = context.getInjectedReference(TYPE_LITERAL); + instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL); } else { - instance = context.getInjectedReference(TYPE_LITERAL, + instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL, new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName)); } if (instance.isResolvable()) { diff --git a/extensions/reactive-mssql-client/deployment/src/main/java/io/quarkus/reactive/mssql/client/deployment/ReactiveMSSQLClientProcessor.java b/extensions/reactive-mssql-client/deployment/src/main/java/io/quarkus/reactive/mssql/client/deployment/ReactiveMSSQLClientProcessor.java index 4dcafa3c6ab01..0ca0afbd230c1 100644 --- a/extensions/reactive-mssql-client/deployment/src/main/java/io/quarkus/reactive/mssql/client/deployment/ReactiveMSSQLClientProcessor.java +++ b/extensions/reactive-mssql-client/deployment/src/main/java/io/quarkus/reactive/mssql/client/deployment/ReactiveMSSQLClientProcessor.java @@ -65,10 +65,13 @@ class ReactiveMSSQLClientProcessor { - private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class), + private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create( + DotName.createSimple(Instance.class), new Type[] { ClassType.create(DotName.createSimple(MSSQLPoolCreator.class.getName())) }, null); private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0]; private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class); + private static final DotName VERTX_MSSQL_POOL = DotName.createSimple(MSSQLPool.class); + private static final Type VERTX_MSSQL_POOL_TYPE = Type.create(VERTX_MSSQL_POOL, Type.Kind.CLASS); @BuildStep @Record(ExecutionTime.RUNTIME_INIT) @@ -207,7 +210,7 @@ private void createPoolIfDefined(MSSQLPoolRecorder recorder, .defaultBean() .addType(Pool.class) .scope(ApplicationScoped.class) - .addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) + .addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) .addInjectionPoint(ClassType.create(DataSourceSupport.class)) .createWith(poolFunction) .unremovable() @@ -222,9 +225,9 @@ private void createPoolIfDefined(MSSQLPoolRecorder recorder, .defaultBean() .addType(io.vertx.mutiny.sqlclient.Pool.class) .scope(ApplicationScoped.class) - .addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) + .addInjectionPoint(VERTX_MSSQL_POOL_TYPE, injectionPointAnnotations(dataSourceName)) .addInjectionPoint(ClassType.create(DataSourceSupport.class)) - .createWith(recorder.mutinyMSSQLPool(poolFunction)) + .createWith(recorder.mutinyMSSQLPool(dataSourceName)) .unremovable() .setRuntimeInit(); diff --git a/extensions/reactive-mssql-client/deployment/src/test/java/io/quarkus/reactive/mssql/client/SamePoolInstanceTest.java b/extensions/reactive-mssql-client/deployment/src/test/java/io/quarkus/reactive/mssql/client/SamePoolInstanceTest.java new file mode 100644 index 0000000000000..4250b0433a2b5 --- /dev/null +++ b/extensions/reactive-mssql-client/deployment/src/test/java/io/quarkus/reactive/mssql/client/SamePoolInstanceTest.java @@ -0,0 +1,27 @@ +package io.quarkus.reactive.mssql.client; + +import jakarta.inject.Inject; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.ClientProxy; +import io.quarkus.test.QuarkusUnitTest; + +public class SamePoolInstanceTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .overrideConfigKey("quarkus.devservices.enabled", "false"); + + @Inject + io.vertx.mutiny.mssqlclient.MSSQLPool mutinyPool; + @Inject + io.vertx.mssqlclient.MSSQLPool pool; + + @Test + public void test() { + Assertions.assertThat(ClientProxy.unwrap(pool)).isSameAs(ClientProxy.unwrap(mutinyPool.getDelegate())); + } +} diff --git a/extensions/reactive-mssql-client/runtime/src/main/java/io/quarkus/reactive/mssql/client/runtime/MSSQLPoolRecorder.java b/extensions/reactive-mssql-client/runtime/src/main/java/io/quarkus/reactive/mssql/client/runtime/MSSQLPoolRecorder.java index bce9406f67eaf..adb8205af7c31 100644 --- a/extensions/reactive-mssql-client/runtime/src/main/java/io/quarkus/reactive/mssql/client/runtime/MSSQLPoolRecorder.java +++ b/extensions/reactive-mssql-client/runtime/src/main/java/io/quarkus/reactive/mssql/client/runtime/MSSQLPoolRecorder.java @@ -10,11 +10,13 @@ import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions; import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions; +import java.lang.annotation.Annotation; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.function.Supplier; +import jakarta.enterprise.inject.Default; import jakarta.enterprise.inject.Instance; import jakarta.enterprise.util.TypeLiteral; @@ -46,7 +48,7 @@ @Recorder public class MSSQLPoolRecorder { - private static final TypeLiteral> TYPE_LITERAL = new TypeLiteral<>() { + private static final TypeLiteral> POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() { }; private static final Logger log = Logger.getLogger(MSSQLPoolRecorder.class); @@ -80,16 +82,31 @@ public MSSQLPool apply(SyntheticCreationalContext context) { } public Function, io.vertx.mutiny.mssqlclient.MSSQLPool> mutinyMSSQLPool( - Function, MSSQLPool> function) { + String dataSourceName) { return new Function, io.vertx.mutiny.mssqlclient.MSSQLPool>() { @Override @SuppressWarnings("unchecked") public io.vertx.mutiny.mssqlclient.MSSQLPool apply(SyntheticCreationalContext context) { - return io.vertx.mutiny.mssqlclient.MSSQLPool.newInstance(function.apply(context)); + DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class); + if (datasourceSupport.getInactiveNames().contains(dataSourceName)) { + throw DataSourceUtil.dataSourceInactive(dataSourceName); + } + + return io.vertx.mutiny.mssqlclient.MSSQLPool.newInstance( + (MSSQLPool) context.getInjectedReference(MSSQLPool.class, + getReactiveDataSourceQualifier(dataSourceName))); } }; } + private static Annotation getReactiveDataSourceQualifier(String dataSourceName) { + if (DataSourceUtil.isDefault(dataSourceName)) { + return Default.Literal.INSTANCE; + } + + return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName); + } + private MSSQLPool initialize(VertxInternal vertx, Integer eventLoopCount, String dataSourceName, DataSourceRuntimeConfig dataSourceRuntimeConfig, @@ -243,9 +260,9 @@ private MSSQLPool createPool(Vertx vertx, PoolOptions poolOptions, MSSQLConnectO SyntheticCreationalContext context) { Instance instance; if (DataSourceUtil.isDefault(dataSourceName)) { - instance = context.getInjectedReference(TYPE_LITERAL); + instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL); } else { - instance = context.getInjectedReference(TYPE_LITERAL, + instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL, new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName)); } if (instance.isResolvable()) { diff --git a/extensions/reactive-mysql-client/deployment/src/main/java/io/quarkus/reactive/mysql/client/deployment/ReactiveMySQLClientProcessor.java b/extensions/reactive-mysql-client/deployment/src/main/java/io/quarkus/reactive/mysql/client/deployment/ReactiveMySQLClientProcessor.java index 2d03f42ef6e7a..30c8cbdbb8367 100644 --- a/extensions/reactive-mysql-client/deployment/src/main/java/io/quarkus/reactive/mysql/client/deployment/ReactiveMySQLClientProcessor.java +++ b/extensions/reactive-mysql-client/deployment/src/main/java/io/quarkus/reactive/mysql/client/deployment/ReactiveMySQLClientProcessor.java @@ -65,10 +65,13 @@ class ReactiveMySQLClientProcessor { - private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class), + private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create( + DotName.createSimple(Instance.class), new Type[] { ClassType.create(DotName.createSimple(MySQLPoolCreator.class.getName())) }, null); private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0]; private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class); + private static final DotName VERTX_MYSQL_POOL = DotName.createSimple(MySQLPool.class); + private static final Type VERTX_MYSQL_POOL_TYPE = Type.create(VERTX_MYSQL_POOL, Type.Kind.CLASS); @BuildStep @Record(ExecutionTime.RUNTIME_INIT) @@ -208,7 +211,7 @@ private void createPoolIfDefined(MySQLPoolRecorder recorder, .defaultBean() .addType(Pool.class) .scope(ApplicationScoped.class) - .addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) + .addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) .addInjectionPoint(ClassType.create(DataSourceSupport.class)) .createWith(poolFunction) .unremovable() @@ -223,9 +226,9 @@ private void createPoolIfDefined(MySQLPoolRecorder recorder, .defaultBean() .addType(io.vertx.mutiny.sqlclient.Pool.class) .scope(ApplicationScoped.class) - .addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) + .addInjectionPoint(VERTX_MYSQL_POOL_TYPE, injectionPointAnnotations(dataSourceName)) .addInjectionPoint(ClassType.create(DataSourceSupport.class)) - .createWith(recorder.mutinyMySQLPool(poolFunction)) + .createWith(recorder.mutinyMySQLPool(dataSourceName)) .unremovable() .setRuntimeInit(); diff --git a/extensions/reactive-mysql-client/deployment/src/test/java/io/quarkus/reactive/mysql/client/SamePoolInstanceTest.java b/extensions/reactive-mysql-client/deployment/src/test/java/io/quarkus/reactive/mysql/client/SamePoolInstanceTest.java new file mode 100644 index 0000000000000..625933e985f2e --- /dev/null +++ b/extensions/reactive-mysql-client/deployment/src/test/java/io/quarkus/reactive/mysql/client/SamePoolInstanceTest.java @@ -0,0 +1,26 @@ +package io.quarkus.reactive.mysql.client; + +import jakarta.inject.Inject; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.ClientProxy; +import io.quarkus.test.QuarkusUnitTest; + +public class SamePoolInstanceTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest(); + + @Inject + io.vertx.mutiny.mysqlclient.MySQLPool mutinyPool; + @Inject + io.vertx.mysqlclient.MySQLPool pool; + + @Test + public void test() { + Assertions.assertThat(ClientProxy.unwrap(pool)).isSameAs(ClientProxy.unwrap(mutinyPool.getDelegate())); + } +} diff --git a/extensions/reactive-mysql-client/runtime/src/main/java/io/quarkus/reactive/mysql/client/runtime/MySQLPoolRecorder.java b/extensions/reactive-mysql-client/runtime/src/main/java/io/quarkus/reactive/mysql/client/runtime/MySQLPoolRecorder.java index 51c911af4836b..b5dd0a518b249 100644 --- a/extensions/reactive-mysql-client/runtime/src/main/java/io/quarkus/reactive/mysql/client/runtime/MySQLPoolRecorder.java +++ b/extensions/reactive-mysql-client/runtime/src/main/java/io/quarkus/reactive/mysql/client/runtime/MySQLPoolRecorder.java @@ -10,6 +10,7 @@ import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions; import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions; +import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -17,6 +18,7 @@ import java.util.function.Function; import java.util.function.Supplier; +import jakarta.enterprise.inject.Default; import jakarta.enterprise.inject.Instance; import jakarta.enterprise.util.TypeLiteral; @@ -47,7 +49,7 @@ @Recorder public class MySQLPoolRecorder { - private static final TypeLiteral> TYPE_LITERAL = new TypeLiteral<>() { + private static final TypeLiteral> POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() { }; public Function, MySQLPool> configureMySQLPool(RuntimeValue vertx, @@ -75,16 +77,30 @@ public MySQLPool apply(SyntheticCreationalContext context) { } public Function, io.vertx.mutiny.mysqlclient.MySQLPool> mutinyMySQLPool( - Function, MySQLPool> function) { + String dataSourceName) { return new Function<>() { @Override @SuppressWarnings("unchecked") public io.vertx.mutiny.mysqlclient.MySQLPool apply(SyntheticCreationalContext context) { - return io.vertx.mutiny.mysqlclient.MySQLPool.newInstance(function.apply(context)); + DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class); + if (datasourceSupport.getInactiveNames().contains(dataSourceName)) { + throw DataSourceUtil.dataSourceInactive(dataSourceName); + } + return io.vertx.mutiny.mysqlclient.MySQLPool.newInstance( + (MySQLPool) context.getInjectedReference(MySQLPool.class, + getReactiveDataSourceQualifier(dataSourceName))); } }; } + private static Annotation getReactiveDataSourceQualifier(String dataSourceName) { + if (DataSourceUtil.isDefault(dataSourceName)) { + return Default.Literal.INSTANCE; + } + + return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName); + } + private MySQLPool initialize(VertxInternal vertx, Integer eventLoopCount, String dataSourceName, @@ -262,9 +278,9 @@ private MySQLPool createPool(Vertx vertx, PoolOptions poolOptions, List context) { Instance instance; if (DataSourceUtil.isDefault(dataSourceName)) { - instance = context.getInjectedReference(TYPE_LITERAL); + instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL); } else { - instance = context.getInjectedReference(TYPE_LITERAL, + instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL, new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName)); } if (instance.isResolvable()) { diff --git a/extensions/reactive-oracle-client/deployment/src/main/java/io/quarkus/reactive/oracle/client/deployment/ReactiveOracleClientProcessor.java b/extensions/reactive-oracle-client/deployment/src/main/java/io/quarkus/reactive/oracle/client/deployment/ReactiveOracleClientProcessor.java index ee5ed3fd99376..6e4ff69b02225 100644 --- a/extensions/reactive-oracle-client/deployment/src/main/java/io/quarkus/reactive/oracle/client/deployment/ReactiveOracleClientProcessor.java +++ b/extensions/reactive-oracle-client/deployment/src/main/java/io/quarkus/reactive/oracle/client/deployment/ReactiveOracleClientProcessor.java @@ -65,10 +65,13 @@ class ReactiveOracleClientProcessor { - private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class), + private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create( + DotName.createSimple(Instance.class), new Type[] { ClassType.create(DotName.createSimple(OraclePoolCreator.class.getName())) }, null); private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0]; private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class); + private static final DotName VERTX_ORACLE_POOL = DotName.createSimple(OraclePool.class); + private static final Type VERTX_ORACLE_POOL_TYPE = Type.create(VERTX_ORACLE_POOL, Type.Kind.CLASS); @BuildStep @Record(ExecutionTime.RUNTIME_INIT) @@ -209,7 +212,7 @@ private void createPoolIfDefined(OraclePoolRecorder recorder, .defaultBean() .addType(Pool.class) .scope(ApplicationScoped.class) - .addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) + .addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) .addInjectionPoint(ClassType.create(DataSourceSupport.class)) .createWith(poolFunction) .unremovable() @@ -224,9 +227,9 @@ private void createPoolIfDefined(OraclePoolRecorder recorder, .defaultBean() .addType(io.vertx.mutiny.sqlclient.Pool.class) .scope(ApplicationScoped.class) - .addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) + .addInjectionPoint(VERTX_ORACLE_POOL_TYPE, injectionPointAnnotations(dataSourceName)) .addInjectionPoint(ClassType.create(DataSourceSupport.class)) - .createWith(recorder.mutinyOraclePool(poolFunction)) + .createWith(recorder.mutinyOraclePool(dataSourceName)) .unremovable() .setRuntimeInit(); diff --git a/extensions/reactive-oracle-client/deployment/src/test/java/io/quarkus/reactive/oracle/client/SamePoolInstanceTest.java b/extensions/reactive-oracle-client/deployment/src/test/java/io/quarkus/reactive/oracle/client/SamePoolInstanceTest.java new file mode 100644 index 0000000000000..721a84c043c76 --- /dev/null +++ b/extensions/reactive-oracle-client/deployment/src/test/java/io/quarkus/reactive/oracle/client/SamePoolInstanceTest.java @@ -0,0 +1,26 @@ +package io.quarkus.reactive.oracle.client; + +import jakarta.inject.Inject; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.ClientProxy; +import io.quarkus.test.QuarkusUnitTest; + +public class SamePoolInstanceTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest(); + + @Inject + io.vertx.mutiny.oracleclient.OraclePool mutinyPool; + @Inject + io.vertx.oracleclient.OraclePool pool; + + @Test + public void test() { + Assertions.assertThat(ClientProxy.unwrap(pool)).isSameAs(ClientProxy.unwrap(mutinyPool.getDelegate())); + } +} diff --git a/extensions/reactive-oracle-client/runtime/src/main/java/io/quarkus/reactive/oracle/client/runtime/OraclePoolRecorder.java b/extensions/reactive-oracle-client/runtime/src/main/java/io/quarkus/reactive/oracle/client/runtime/OraclePoolRecorder.java index 54ef9588855e6..b33346cb154e5 100644 --- a/extensions/reactive-oracle-client/runtime/src/main/java/io/quarkus/reactive/oracle/client/runtime/OraclePoolRecorder.java +++ b/extensions/reactive-oracle-client/runtime/src/main/java/io/quarkus/reactive/oracle/client/runtime/OraclePoolRecorder.java @@ -4,11 +4,13 @@ import static io.quarkus.credentials.CredentialsProvider.USER_PROPERTY_NAME; import static io.quarkus.reactive.datasource.runtime.UnitisedTime.unitised; +import java.lang.annotation.Annotation; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.function.Supplier; +import jakarta.enterprise.inject.Default; import jakarta.enterprise.inject.Instance; import jakarta.enterprise.util.TypeLiteral; @@ -41,7 +43,7 @@ @Recorder public class OraclePoolRecorder { - private static final TypeLiteral> TYPE_LITERAL = new TypeLiteral<>() { + private static final TypeLiteral> POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() { }; private static final Logger log = Logger.getLogger(OraclePoolRecorder.class); @@ -71,16 +73,30 @@ public OraclePool apply(SyntheticCreationalContext context) { } public Function, io.vertx.mutiny.oracleclient.OraclePool> mutinyOraclePool( - Function, OraclePool> function) { + String dataSourceName) { return new Function<>() { @SuppressWarnings("unchecked") @Override public io.vertx.mutiny.oracleclient.OraclePool apply(SyntheticCreationalContext context) { - return io.vertx.mutiny.oracleclient.OraclePool.newInstance(function.apply(context)); + DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class); + if (datasourceSupport.getInactiveNames().contains(dataSourceName)) { + throw DataSourceUtil.dataSourceInactive(dataSourceName); + } + return io.vertx.mutiny.oracleclient.OraclePool.newInstance( + (OraclePool) context.getInjectedReference(OraclePool.class, + getReactiveDataSourceQualifier(dataSourceName))); } }; } + private static Annotation getReactiveDataSourceQualifier(String dataSourceName) { + if (DataSourceUtil.isDefault(dataSourceName)) { + return Default.Literal.INSTANCE; + } + + return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName); + } + private OraclePool initialize(VertxInternal vertx, Integer eventLoopCount, String dataSourceName, @@ -211,9 +227,9 @@ private OraclePool createPool(Vertx vertx, PoolOptions poolOptions, OracleConnec SyntheticCreationalContext context) { Instance instance; if (DataSourceUtil.isDefault(dataSourceName)) { - instance = context.getInjectedReference(TYPE_LITERAL); + instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL); } else { - instance = context.getInjectedReference(TYPE_LITERAL, + instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL, new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName)); } if (instance.isResolvable()) { diff --git a/extensions/reactive-pg-client/deployment/src/main/java/io/quarkus/reactive/pg/client/deployment/ReactivePgClientProcessor.java b/extensions/reactive-pg-client/deployment/src/main/java/io/quarkus/reactive/pg/client/deployment/ReactivePgClientProcessor.java index 9f87c5535811b..d8b83bcaf3408 100644 --- a/extensions/reactive-pg-client/deployment/src/main/java/io/quarkus/reactive/pg/client/deployment/ReactivePgClientProcessor.java +++ b/extensions/reactive-pg-client/deployment/src/main/java/io/quarkus/reactive/pg/client/deployment/ReactivePgClientProcessor.java @@ -66,10 +66,13 @@ class ReactivePgClientProcessor { - private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class), + private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create( + DotName.createSimple(Instance.class), new Type[] { ClassType.create(DotName.createSimple(PgPoolCreator.class.getName())) }, null); private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0]; private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class); + private static final DotName VERTX_PG_POOL = DotName.createSimple(PgPool.class); + private static final Type VERTX_PG_POOL_TYPE = Type.create(VERTX_PG_POOL, Type.Kind.CLASS); @BuildStep NativeImageConfigBuildItem config() { @@ -213,7 +216,7 @@ private void createPoolIfDefined(PgPoolRecorder recorder, .defaultBean() .addType(Pool.class) .scope(ApplicationScoped.class) - .addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) + .addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) .addInjectionPoint(ClassType.create(DataSourceSupport.class)) .createWith(poolFunction) .unremovable() @@ -223,14 +226,15 @@ private void createPoolIfDefined(PgPoolRecorder recorder, syntheticBeans.produce(pgPoolBeanConfigurator.done()); + // the Mutiny pool is created by using the Vertx pool ExtendedBeanConfigurator mutinyPgPoolConfigurator = SyntheticBeanBuildItem .configure(io.vertx.mutiny.pgclient.PgPool.class) .defaultBean() .addType(io.vertx.mutiny.sqlclient.Pool.class) .scope(ApplicationScoped.class) - .addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName)) + .addInjectionPoint(VERTX_PG_POOL_TYPE, injectionPointAnnotations(dataSourceName)) .addInjectionPoint(ClassType.create(DataSourceSupport.class)) - .createWith(recorder.mutinyPgPool(poolFunction)) + .createWith(recorder.mutinyPgPool(dataSourceName)) .unremovable() .setRuntimeInit(); diff --git a/extensions/reactive-pg-client/deployment/src/test/java/io/quarkus/reactive/pg/client/SamePoolInstanceTest.java b/extensions/reactive-pg-client/deployment/src/test/java/io/quarkus/reactive/pg/client/SamePoolInstanceTest.java new file mode 100644 index 0000000000000..e3ed8404f95f1 --- /dev/null +++ b/extensions/reactive-pg-client/deployment/src/test/java/io/quarkus/reactive/pg/client/SamePoolInstanceTest.java @@ -0,0 +1,27 @@ +package io.quarkus.reactive.pg.client; + +import static org.assertj.core.api.Assertions.*; + +import jakarta.inject.Inject; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.ClientProxy; +import io.quarkus.test.QuarkusUnitTest; + +public class SamePoolInstanceTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest(); + + @Inject + io.vertx.mutiny.pgclient.PgPool mutinyPool; + @Inject + io.vertx.pgclient.PgPool pool; + + @Test + public void test() { + assertThat(ClientProxy.unwrap(pool)).isSameAs(ClientProxy.unwrap(mutinyPool.getDelegate())); + } +} diff --git a/extensions/reactive-pg-client/runtime/src/main/java/io/quarkus/reactive/pg/client/runtime/PgPoolRecorder.java b/extensions/reactive-pg-client/runtime/src/main/java/io/quarkus/reactive/pg/client/runtime/PgPoolRecorder.java index 554614a412d1f..2dacfe32dcfa3 100644 --- a/extensions/reactive-pg-client/runtime/src/main/java/io/quarkus/reactive/pg/client/runtime/PgPoolRecorder.java +++ b/extensions/reactive-pg-client/runtime/src/main/java/io/quarkus/reactive/pg/client/runtime/PgPoolRecorder.java @@ -10,12 +10,14 @@ import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions; import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions; +import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.function.Supplier; +import jakarta.enterprise.inject.Default; import jakarta.enterprise.inject.Instance; import jakarta.enterprise.util.TypeLiteral; @@ -46,7 +48,7 @@ @Recorder public class PgPoolRecorder { - private static final TypeLiteral> TYPE_LITERAL = new TypeLiteral<>() { + private static final TypeLiteral> PG_POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() { }; public Function, PgPool> configurePgPool(RuntimeValue vertx, @@ -74,16 +76,30 @@ public PgPool apply(SyntheticCreationalContext context) { } public Function, io.vertx.mutiny.pgclient.PgPool> mutinyPgPool( - Function, PgPool> function) { + String dataSourceName) { return new Function<>() { @SuppressWarnings("unchecked") @Override public io.vertx.mutiny.pgclient.PgPool apply(SyntheticCreationalContext context) { - return io.vertx.mutiny.pgclient.PgPool.newInstance(function.apply(context)); + DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class); + if (datasourceSupport.getInactiveNames().contains(dataSourceName)) { + throw DataSourceUtil.dataSourceInactive(dataSourceName); + } + + return io.vertx.mutiny.pgclient.PgPool.newInstance( + (PgPool) context.getInjectedReference(PgPool.class, getReactiveDataSourceQualifier(dataSourceName))); } }; } + private static Annotation getReactiveDataSourceQualifier(String dataSourceName) { + if (DataSourceUtil.isDefault(dataSourceName)) { + return Default.Literal.INSTANCE; + } + + return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName); + } + private PgPool initialize(VertxInternal vertx, Integer eventLoopCount, String dataSourceName, @@ -251,9 +267,9 @@ private PgPool createPool(Vertx vertx, PoolOptions poolOptions, List context) { Instance instance; if (DataSourceUtil.isDefault(dataSourceName)) { - instance = context.getInjectedReference(TYPE_LITERAL); + instance = context.getInjectedReference(PG_POOL_CREATOR_TYPE_LITERAL); } else { - instance = context.getInjectedReference(TYPE_LITERAL, + instance = context.getInjectedReference(PG_POOL_CREATOR_TYPE_LITERAL, new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName)); } if (instance.isResolvable()) {