java – spring failover jdbc


There are two databases (PostgreSQL) that completely duplicate each other. In our api application we used to use one datasource, because there was no replica and no one worried about fault tolerance, but now we need to switch between datasources at runtime, depending on the availability of a particular datasource. … On the Internet, I found information about this: for example, ha-jdbc , it was relevant for 2013-2014. I also read various information about @Primary and secondary datasources, but will they switch automatically …

The question is, what are the best practices for dynamically switching a datasource if one of them is unavailable? If you only need to READ from tables.

PS the project uses spring-data-jpa v.2.1.1.

PSS, or perhaps this is generally done at the database level (by some postgresql technologies), and not by the API


You need an XA driver. And adjust it accordingly. More details on this question have already been answered here

Scroll to Top