title: Can I specify *source* IP address for a JDBC connection?
body: <p>I have a Java application that connects to a database with a JDBC driver. It happens to be a Postgres instance, but I'm looking for a general solution if it exist.</p>
  
  <p>The server on which the application runs has multiple IP address, all in the same subnet. The JDBC connection is established using the 'primary' IP of the server. </p>
  
  <p>I can <a href="https://serverfault.com/a/182594/81170">play with Linux networking stack</a> to have outgoing connections to the database use the secondary IP address.</p>
  
  <p>Is there a way to programatically specify the <strong>source IP</strong> address will be used to to my JDBC connection? </p>
  
  <hr>
  
  <p><em>Context: We want to move the application without touching the database, which has rules restricting IP address it will accept connections from.</em></p>
  
  <p><em>The only hack I see would be making an SSL connection, and return a new Socket bound to the secondary IP from a custom <a href="https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/SSLSocketFactory.html" rel="nofollow noreferrer">SSLSocketFactory</a>.</em></p>