title: How to configure connection failover for a PostgreSQL Hot Standby setup in a JavaEE application?
body: <p>I have two Linux servers (<code>A</code> and <code>B</code>) with a <code>PostgreSQL 9.5</code> database installed. I configured <em>hot standby</em> mode as described in the <a href="https://www.postgresql.org/docs/9.5/static/hot-standby.html" rel="noreferrer">documentation</a>. In this setup, <code>A</code> is configured as master, <code>B</code> in hot standby mode. This is working well and behaves as expected.</p>
  
  <p>Now, I want to connect an independent <code>Java EE</code> application (running on a different machine) via <code>Hibernate</code> / <code>JDBC</code> via a <code>TomEE</code> datasource to this database setup. </p>
  
  <p>The <a href="https://jdbc.postgresql.org/documentation/head/connect.html" rel="noreferrer">PostgreSQL driver</a> documentation states, that multiple hosts can be specified in the jdbc connection url:</p>
  
  <pre><code>jdbc:postgresql://host1:port1,host2:port2/database
  </code></pre>
  
  <p>So my questions are:</p>
  
  <ol>
  <li>If <code>A</code> is down and <code>B</code> is switched manually to normal operation mode, is my application still able to proceed with database operation with a jdbc connection url as stated above?</li>
  <li>Do I have to configure other parameters / libraries?</li>
  </ol>
  
  <p>Note: From various sources I learned, that <code>PostgreSQL</code> does not support automatic failover (unless third-party software is involved in the process - see comments below). For this reason, failover needs to performed manually, which is ok for this particular use-case.</p>
  
  <p><strong>EDIT-1:</strong></p>
  
  <p>I decided to test <code>pgBouncer</code> (as suggested in the comments) for a workaround. It works well for my use-case. I wrote a watchdog script, which automates the manual steps:</p>
  
  <ol>
  <li>Continously check, if <code>A</code> is still alive and listens for incoming connections.</li>
  <li>In case of failover, switch <code>B</code> to normal operation mode and let it become the new master and restart the service.</li>
  <li>Change the <code>pgBouncer</code> settings to point to <code>B</code> instead of <code>A</code> and restart the service.</li>
  </ol>
  
  <p>However, I would be still interested, if anybody has experiences without third party software?</p>