Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Micronaut 2.x problem with JdbcRepository multiple data sources #657

Closed
maryam-nikoukar opened this issue Jul 14, 2020 · 4 comments
Closed
Assignees
Labels
status: awaiting validation Waiting to be validated as a real issue

Comments

@maryam-nikoukar
Copy link

Hi, we are upgrading to Micronaut 2.x and we are having some issue on our data layer when using javax.transaction.Transactional annotation. We have two data sources on application.yml one for read only operations and one for read and write.

datasources:
  main:
    pool-name: 'main-pool'
    url: 'the-url'
    username: 'username'
    password: 'password'
    dialect: POSTGRES
    driver-class-name: 'org.postgresql.Driver'
  read-only:
    pool-name: 'readonly-pool'
    url: 'the-url'
    username: 'username'
    password: 'password'
    dialect: POSTGRES
    driver-class-name: 'org.postgresql.Driver'
    read-only: true

Our repos are annotated with io.micronaut.data.jdbc.annotation.JdbcRepository and also io.micronaut.data.annotation.Repository (with the name of the data source).

@Repository("main")
@JdbcRepository(dialect = Dialect.POSTGRES)
public abstract class ReadWriterRepository 
       
        {


   @Transactional
   public Entity upsert(
           Entity entity) {
       ...
   }

and

@Repository("read-only")
@JdbcRepository(dialect = Dialect.POSTGRES)
public interface ReaderRepository extends GenericRepository<E, ID>
      	 {}

The issue is when we can the method (upsert) annotaed with javax.transaction.Transactional and we are getting : "Multiple possible bean candidates found: [io.micronaut.transaction.jdbc.DataSourceTransactionManager, io.micronaut.transaction.jdbc.DataSourceTransactionManager]" . I was wondening if anyone has encoured the same issue and how to fix it? is there any incomatibility with micronaut data (1.1.1) and Micornaut 2.x?

When we debug in DefaultBeanContext.findConcreteCandidate there are two beans one for "ready-only" data source and one for "main" data source. before upgrading to Micronaut 2.x the data sources was automatically picked up by io.micronaut.data.annotation.Repository("data-source-name"). and the transactional manager was automatically resolved to the correct bean

@graemerocher graemerocher transferred this issue from micronaut-projects/micronaut-core Jul 14, 2020
@graemerocher graemerocher added the status: awaiting validation Waiting to be validated as a real issue label Jul 14, 2020
@graemerocher graemerocher self-assigned this Jul 14, 2020
@sillyoldman
Copy link

sillyoldman commented Jul 15, 2020

I had similar issues. I was able to resolve it by using @TransactionAdvice.

@ Transactional
@TransactionAdvice("main")
public Entity upsert(
Entity entity) {
...
}

Based on the code that debuggged and followed - For each Datasource, a DataSourceTrasansactionManager Bean is created. However it is not directly associated with the Datasource. Thus while@Repository is able to specify the datasource to use, it still finds 2 beans of DataSourceTrasansactionManager in the context. Using transaction advice, allows you to specify the transaciton manager to use.

@sillyoldman
Copy link

To add - the Interceptor seems to look for TransactionAdvice on the method. Although the annotation itself can be applied on class, anything applied on class is not checked currnetly. Perhaps it is by design.

@graemerocher
Copy link
Contributor

That is currently the correct fix. Thanks @sillyoldman

We need more documentation on this area

@maryam-nikoukar
Copy link
Author

adding the TransactionAdvice annotation fixed the issue for me. Thank you very much for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting validation Waiting to be validated as a real issue
Projects
None yet
Development

No branches or pull requests

3 participants