How to use different datasources for read and write queries in Quarkus?

Viewed 621

I need to separate reader/writer PostgreSQL instances. I have one write replica and several read replicas.

Quarkus supports adding multiple data sources, however it is not clear how to make Panache/Hibernate pick the right one for different queries.

Question: How to configure Quarkus to use different data sources for read/write access?

1 Answers

We use the following in our properties file to distinguish which DB datasource you need to pick up. Then you separate in different packages code pointing to different datasources.

quarkus.hibernate-orm."db".packages=com.mycompany.outgoing.db
quarkus.hibernate-orm."push".packages=com.mycompany.outgoing.push

I think you´ll need to have subclasses pointing to the same BaseEntityof some sort to distinguish between datasources.

Other option I can think of, that I haven´t used yet, is injecting 2 different EntityManager in your repository class and manage your repository from the EntityManager directly.

@Inject
@PersistenceUnit("db") 
EntityManager entityManager;

@Inject
@PersistenceUnit("push") 
EntityManager entityManagerPush;
Related