0

I have a spring boot REST API application for a project with 2 different setups ( =2 different jars). Both need to be realized, but I have no idea on how to do it or what the best way would be.

Both setups need to have a connection with an online database (on a server; AWS) and a connection with an offline or local database (running on the machine/pc itself). It is the offline or local connection that is different between both setups.


Setup 1:
When the application is started it needs to connect to the online database. When an error occurs or connection to the online database is lost, it needs to connect to the offline/local database.

It is not required that that it will reconnect with the online database after an error occurred or the connection was lost.

Setup 2:
When the application is started it needs to connect to both the online and the offline database. So when the user does a post to the REST API, both the online and the offline database need to be updated (unless an error occurs or the connection to the online database is lost). If the user just does a get request, it is preferred to get the data from the online database, unless an error occurs or the connection to the online database is lost, than it can use the offline database.

It is not required that that it will reconnect with the online database after an error occurred or the connection was lost, but in this setup it would be nice. Synchronizing the data after connection has been established again isn't required either (but would maybe be a nice feature).


I have seen a similar post where the solution was to use ha-jdbc but it is an old post... Maven just doesn't find the dependency when I try adding it to my pom.xml file.

After some more searching and trying I was able to add ha-jdbc to my pom.xml. What I had to do was add a repository that contained ha-jdbc.

<project>
    <repositories>
        <repository>
            <id>jbossrepository</id>
            <name>jbossrepository</name>
            <url>https://repository.jboss.org/nexus/content/repositories/thirdparty-releases/</url>
        </repository>
    </repositories>
    <dependency>
        <dependency>
            <groupId>net.sf.ha-jdbc</groupId>
            <artifactId>ha-jdbc</artifactId>
            <version>3.0.3</version>
        </dependency>
    </dependency>
</project>

Yet to see if I can get it to work and if it is what I am looking for...
ha-jdbc isn't worth trying, one error or problem after the other, it's just old java too...


Versions:
- java: 1.8
- org.springframework.boot (spring-boot-starter-parent): 2.1.8.RELEASE
- Database (online & offline): PostgreSQL 11.5

3
  • And why wouldn't ha-jdbc work? It has nothing to do with Spring Boot it is just an alternate datasource implemenation which you need to configure. Works with any framework. Commented Nov 5, 2019 at 11:20
  • here you go baeldung.com/spring-data-jpa-multiple-databases Commented Nov 5, 2019 at 11:38
  • @ErangaGamagedara I already looked at that, but it isn't clear to me when it uses db1 and when it switches to db2. To me this looks like bot databases are completely different and are both needed at all time. In my case, both databases are a copy of each other but not every setup needs constant access to both (as I explained in my post). Maybe you can tell me more about this? Commented Nov 5, 2019 at 11:45

1 Answer 1

0

Take a look this example code. There you can find already implemented a working solution. The logic behind it is EnableJpaRepositories which enables it based on packages (basePackages)

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, I had seen this already and it does come close to what I need, but this is when both databases are different. In my case they are the same and for 'setup 2' I need switch from one to the other if an error occurred or the connection was lost. I don't see how I can change your example to my specific needs... In this example the repository and domain is different too, while for me it is exactly the same for both databases... I'm not really willing to duplicate everything just for connecting with another database.
2nd database has different connection string, username and passwords?
Different host yes, the user and password are the same. the shema and data/content is a copy, so they have the same tables and the same sequences, the same data...
which database? (which vendor? is it Postgres?)
For example if you are using postgreSQL you can pass your instances to connection string as jdbc:postgresql://host1:port1,host2:port2/database which will guaranty that it uses bost hosts/instances and if one of them fails it connects other
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.