From: Mistina Michal <Michal.Mistina@virte.sk>
To: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: Fail-over PG server in connection string - syntax ?
Date: Mon, 22 Jul 2013 15:35:45 +0000
Message-ID: <4da286cd15324df59679fa18960e58d3@Electra.virte.intra> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>
Hi all.
We have 2 datacenters in diferent locations with public IP addresses each
from different subnets. We would like to use Posgtres JDBC driver fail-over
capability to switch over java client to secondary datacenter if the first
one is unavailable. We need only basic functionality. We cannot use
pgpool-II or something similar at the server side because all these type of
softwares use one virtual IP address which in many cases is from one subnet.
There should be fail-over capability available in the JDBC version 9.2-1000
according to changelog:
http://jdbc.postgresql.org/changes.html#version_9.2-1000
The documentation doesn't mention how to build fail-over connection string
at all. I've found in following mailing list thread mentioning the structure
of the connection string:
http://postgresql.1045698.n5.nabble.com/Re-Failover-Datasource-td4496411.htm
l#a5736270
I tried fail-over by using the version 9.2-1002 in the following example.
I tried to build easy test java script according to this site:
http://www.dotnetfunda.com/Blogs/Niladri.Biswas/3743/connect-to-postgresql-f
rom-java-using-jdbc-driver
The java file then looks like this.
import java.sql.*;
public class JavaPostgreSQLConnectivityTest
{
public static void main(String[] args)
{
String connectionString =
"jdbc:postgresql://pgsqldc1:5432/test,pgsqldc2:5432/test";
String userName = "postgres";
String password = "postgres";
try
{
Class.forName("org.postgresql.Driver"); //specify the JDBC
driver for PostgreSQL
Connection conn = DriverManager.getConnection(connectionString,
userName, password);
System.out.println("Connection properly established");
Statement st = conn.createStatement();
ResultSet rs =
st.executeQuery("SELECT * FROM mytable ORDER BY created DESC LIMIT 5");
while
(rs.next()) {
System.out.print("Column 1 returned ");
System.out.println(rs.getString(1));
}
rs.close();
st.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
When I ran the java compiled class I got this.
D:\temp\Scripts>java -cp postgresql-9.2-1003.jdbc4.jar;
JavaPostgreSQLConnectivityTest
org.postgresql.util.PSQLException: FATAL: database "test,pgsqldc2:5432/test"
does not exist
at
org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages(ConnectionF
actoryImpl.java:572)
at
org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFa
ctoryImpl.java:177)
at
org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:
64)
at
org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.
java:136)
at
org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.
java:29)
at
org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnecti
on.java:21)
at
org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.
java:31)
at
org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at
JavaPostgreSQLConnectivityTest.main(JavaPostgreSQLConnectivityTest.java:15)
What is the correct syntax for building connection string? Should the
fail-over within postgres-jdbc driver work?
Best regards,
Michal Mistina
Attachments:
smime.p7s (application/pkcs7-signature, 3.0K) download
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: Michal.Mistina@virte.sk
Subject: Re: Fail-over PG server in connection string - syntax ?
In-Reply-To: <4da286cd15324df59679fa18960e58d3@Electra.virte.intra>
References: <4da286cd15324df59679fa18960e58d3@Electra.virte.intra>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox