micronaut-data icon indicating copy to clipboard operation
micronaut-data copied to clipboard

Multiple datasources configuration not working

Open rohandodrajka opened this issue 1 year ago • 5 comments

We are trying to create 2 datasource beans in our Micronaut appilcation. The beans are getting created successfully as we are able to obtain them from application context. Here is how we created Datasource beans:

@Bean
 @Singleton
 @Named("appshellDatasource")
 public DataSource appshellDatasource(){
        return new HikariDataSource(getHikariConfig(podConfigUtil.getAppshellSchemaUser(), podConfigUtil.getAppshellPwd()));
    }

@Bean
@Singleton
@Named("common")
public DataSource commonDataSource() {
        return new HikariDataSource(getHikariConfig(podConfigUtil.getCommonSchemaUser(), podConfigUtil.getCommonPwd()));

    } 

But, when we are trying to query the database with any of the datasources we are getting the below exception:

24-04-01 17:49:35,271 ERROR [default-nioEventLoopGroup-1-3] i.m.h.s.RouteExecutor:  - msg: Unexpected error occurred: Error instantiating bean of type  [io.micronaut.data.connection.jdbc.advice.ContextualConnection$Intercepted]Message: Multiple possible bean candidates found: [DefaultDataSourceConnectionOperations, DefaultDataSourceConnectionOperations]

Please find the detailed stack trace attached:
Stack trace.txt

This is the related discussion on Slack channel #gcn-micronaut-help: Conversation Link

rohandodrajka avatar Apr 16 '24 07:04 rohandodrajka

@radovanradic please look into this

graemerocher avatar Apr 22 '24 12:04 graemerocher

I did investigate it before, and the problem is that:

@Prototype
public final class ContextualConnectionInterceptor implements MethodInterceptor<Connection, Object> {

    private final ConnectionOperations<Connection> connectionOperations;

    /**
     * Default constructor.
     *
     * @param beanContext The bean context
     * @param qualifier   The qualifier
     */
    @Internal
    ContextualConnectionInterceptor(BeanContext beanContext, Qualifier<ConnectionOperations> qualifier) {
        connectionOperations = beanContext.getBean(Argument.of(ConnectionOperations.class, Connection.class), qualifier);
    }

Being injected without the qualifier.

dstepanov avatar Apr 22 '24 13:04 dstepanov

@dstepanov have you tried annotating @Parameter on the qualifier? Otherwise seems like a regression in core

graemerocher avatar Apr 22 '24 15:04 graemerocher

No, but I saw in debugger that the qualifier is replaced with null in the bean context.

dstepanov avatar Apr 22 '24 16:04 dstepanov

We run into the same problem. If multiple datasources are defined but none is called 'default' any bean with an around advice (@Validated, @Transactional) causes the whole thing to break as we get multiple candidate beans. When we defined a default datasource it fixes itself but I'm not sure about the implications.

Is it known when this regression was introduced? We'd like to roll back to the version before that as we've just upgraded all our code to MN4 (yes, we're late)

meredrica avatar Apr 29 '24 13:04 meredrica