1

My question is similar to the one in this 2 year old post:

Is there a way to handle dynamic connexion to datasource with quarkus?

I have say 10 different DBs, all of them are in Postgres and they even have the same user and password, so the only thing that changes is the database name.

I have an endpoint similar to /{databasename}/customers, so I'd like to be able to retrieve the list of customers from the database name that is passed as a parameter.

I know I might try to write an extension to do that, but I have not been able to do so, and I'm not sure that I really want to modify the framework to do this.

I'd like to understand what the options are to do something like this

Henry
  • 67
  • 5
  • `quarkus.datasource.jdbc.url` is not fixed at build time. so it should be overridable at runtime. changing it on runtime does not cover your problem? – Kerim Oguzcan Yenidunya May 18 '22 at 13:48
  • How do you override that at runtime programmatically? – Henry May 18 '22 at 14:16
  • I tested with this: System.setProperty("quarkus.datasource.jdbc.url", "quarkus_test2"); and nop, it does not work – Henry May 18 '22 at 15:06
  • Look at [Quarkus' multitenancy](https://quarkus.io/guides/hibernate-orm#multitenancy). What you are describing seems to fit this model nicely – Paul Stoner Aug 26 '22 at 18:10

1 Answers1

0

I thought that something similar to the code snippet below might help to dynamically connect to a DS, but there's not way for me to make it work because this is a way to programmatically set up the connection once the application is starting up:

        AgroalDataSourceConfigurationSupplier configuration = new AgroalDataSourceConfigurationSupplier()
                .dataSourceImplementation( AgroalDataSourceConfiguration.DataSourceImplementation.AGROAL )
                .metricsEnabled( false )
                .connectionPoolConfiguration( cp -> cp
                        .minSize( 5 )
                        .maxSize( 20 )
                        .initialSize( 10 )
                        .connectionValidator( AgroalConnectionPoolConfiguration.ConnectionValidator.defaultValidator() )
                        .acquisitionTimeout( Duration.ofSeconds( 5 ) )
                        .leakTimeout( Duration.ofSeconds( 5 ) )
                        .validationTimeout( Duration.ofSeconds( 50 ) )
                        .reapTimeout( Duration.ofSeconds( 500 ) )
                        .connectionFactoryConfiguration( cf -> cf
                                .jdbcUrl( "jdbc:postgresql://localhost/quarkus_test3" )
//                                .connectionProviderClassName( "org.h2.Driver" )
                                .autoCommit( false )
                                .jdbcTransactionIsolation( AgroalConnectionFactoryConfiguration.TransactionIsolation.SERIALIZABLE )
                                .principal( new NamePrincipal( "postgres" ) )
                                .credential( new SimplePassword( "postgres123" ) )
                        )
                );

        try ( AgroalDataSource dataSource = AgroalDataSource.from( configuration ) ) {
            Connection connection = dataSource.getConnection();
            connection.close();
        } catch ( SQLException e ) {
            System.out.println( "Oops! " + e.getMessage() );
        }
Henry
  • 67
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 18 '22 at 20:03