How to access the same DB from 2 different spring boot applications

I have 2 spring microservice Applications and want them to access the same DB. But both Applications create a different databases when i run them. The Application.yml files:

Service 1:

server.port: 8002  logging:   level:     org:       springframework:         jdbc:           core:             DEBUG  spring:   application:     name: movie   datasource:     url: jdbc:h2:mem:movie_service     driver-class-name: org.h2.Driver   h2:     console:       enabled: true  eureka:   client:     serviceUrl:       defaultZone: ${EUREKA_URI:http://localhost:8001/eureka}     registryFetchIntervalSeconds: 1   instance:     leaseRenewalIntervalInSeconds: 1     #preferIpAddress: true eureka.instance.prefer-ip-address: false  

Service 2:

server.port: 8003  spring:   application.name: client   datasource:     url: jdbc:h2:mem:movie_service     driver-class-name: org.h2.Driver   h2:     console:       enabled: true  logging:   level:     org:       springframework:         jdbc:           core:             DEBUG  eureka:   client:     serviceUrl:       defaultZone: ${EUREKA_URI:http://localhost:8001/eureka}     registryFetchIntervalSeconds: 1   instance:     leaseRenewalIntervalInSeconds: 1     #preferIpAddress: true eureka.instance.prefer-ip-address: false 

I thought by providing the same URL, one service will create the DB and the other one will just use it.

Add Comment
1 Answer(s)

H2 is an in memory database, use a database that allows multiple users.

UPDATE:

actually, I was not precise. H2 has a server mode, but you are running the database embedded in your application.

You need the database as a service and then you can connect from your microservice applications to that running database.

As a side note, it is a common antipattern to have different microservices accessing the same database.

The database – if it is shared at all – should only be shared by instances of one microservice component.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.