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

Connect micronaut application with both Mongo DB & MySQL

Open prashantcodecraft opened this issue 1 year ago • 8 comments

Issue description

I need to connect micronaut with both MySQL and Mongo DB where in the Mongo DB will be primary and MySql is just to fetch some results. If I am using JPA and creating a lambda for the same following error occurs on start of it :

`

Caused by: io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [io.micronaut.data.mongodb.transaction.MongoSynchronousTransactionManagerImpl, io.micronaut.transaction.hibernate5.HibernateTransactionManager]


`

prashantcodecraft avatar Jul 24 '23 10:07 prashantcodecraft

Try to rename the datasource to something other than the default.

dstepanov avatar Jul 24 '23 11:07 dstepanov

If I try renaming database then jdbc hikari initialization fails, so basically JPA is not able to connect to MySQL instance on start up.

prashantcodecraft avatar Jul 24 '23 11:07 prashantcodecraft

Hmm that's strange, can you please create a sample app to reproduce the problem?

dstepanov avatar Jul 24 '23 12:07 dstepanov

Sure will create a dummy project.

Just by changing default DB name , I get following :

Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [io.micronaut.data.operations.PrimaryRepositoryOperations] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor). at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2805) at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1617) at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:867) at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:859)

prashantcodecraft avatar Jul 24 '23 13:07 prashantcodecraft

You might need to use @Repository("mydb")

dstepanov avatar Jul 24 '23 13:07 dstepanov

Even adding the correct db name , I get this :

No bean of type [io.micronaut.data.operations.PrimaryRepositoryOperations] exists for the given qualifier: @Named('mydb').

prashantcodecraft avatar Jul 24 '23 13:07 prashantcodecraft

I think the problem is or connecting MySQL I need following dependencies :

<dependency> <groupId>io.micronaut.data</groupId> <artifactId>micronaut-data-hibernate-jpa</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>io.micronaut.sql</groupId> <artifactId>micronaut-jdbc-hikari</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>

For connecting Mongo I need following : 

` <dependency>
  <groupId>io.micronaut.data</groupId>
  <artifactId>micronaut-data-mongodb</artifactId>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongodb-driver-sync</artifactId>
  <scope>runtime</scope>
</dependency>`

And it appears even micronaut launcher is not allowing both in one: 

image

prashantcodecraft avatar Jul 24 '23 13:07 prashantcodecraft

I think this reproduces the issue, uses h2 and mongodb. Calling controller http://localhost:8080/login/create will reproduce the issue. mn3-mongo-hibernate.zip Same happens in Micronaut 4 as well as in tests (when transactional = true, and it will find two trans managers).

radovanradic avatar Jul 24 '23 13:07 radovanradic