Re: Multiple databases?

From: Mikko Tiihonen <mikko(dot)tiihonen(at)nitor(dot)fi>
To: Turbo Fredriksson <turbo(at)bayour(dot)com>, List <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Multiple databases?
Date: 2017-09-15 13:18:13
Message-ID: HE1PR0701MB2826CD166D19B2EBE1601DC7E66C0@HE1PR0701MB2826.eurprd07.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

To iterate on the response Vladimir already gave: The documentation https://jdbc.postgresql.org/documentation/94/connect.html has under the "Connection Fail-over" section an example how to set up the fail-over and load balancing correctly:

--clip---

For example an application can create two connection pools. One data source is for writes, another for reads.

The write pool limits connections only to master node:

jdbc:postgresql://node1,node2,node3/accounting?targetServerType=master

And read pool balances connections between slaves nodes, but allows connections also to master if no slaves are available:

jdbc:postgresql://node1,node2,node3/accounting?targetServerType=preferSlave&loadBalanceHosts=true

--clip--

So you just make your application code select the correct pool to use and you should have load balancing for reads and fail-over for writes.

As Vladimir already explained the load balancing only occurs when opening new connections, so do set your pool to close idle connections.

And if you can choose which connection pool to use I would like to recommend the actively maintained https://github.com/brettwooldridge/HikariCP.

-Mikko

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Daniel Gustafsson 2017-09-15 14:19:18 Re: Statement-level rollback
Previous Message Dave Cramer 2017-09-15 11:16:11 Re: Multiple databases?