pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Fail-over PG server in connection string - syntax ?
2+ messages / 2 participants
[nested] [flat]

* Fail-over PG server in connection string - syntax ?
@ 2013-07-22 15:35 Mistina Michal <Michal.Mistina@virte.sk>
  2013-07-23 06:23 ` Re: Fail-over PG server in connection string - syntax ? Chen Huajun <chenhj@cn.fujitsu.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Mistina Michal @ 2013-07-22 15:35 UTC (permalink / raw)
  To: pgsql-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

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Fail-over PG server in connection string - syntax ?
  2013-07-22 15:35 Fail-over PG server in connection string - syntax ? Mistina Michal <Michal.Mistina@virte.sk>
@ 2013-07-23 06:23 ` Chen Huajun <chenhj@cn.fujitsu.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Chen Huajun @ 2013-07-23 06:23 UTC (permalink / raw)
  To: Mistina Michal <Michal.Mistina@virte.sk>; +Cc: pgsql-jdbc

hi Michal,


 >What is the correct syntax for building connection string? Should the fail-over within postgres-jdbc driver work?

The correct syntax is as the following
jdbc:postgresql://host1:port1,host2:port2/test


 >                  String connectionString = "jdbc:postgresql://pgsqldc1:5432/test,pgsqldc2:5432/test";

In the above sample,the correct connection string should looks like that

String connectionString = "jdbc:postgresql://pgsqldc1:5432,pgsqldc2:5432/test";


Best regards,
Chen Huajun





-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2013-07-23 06:23 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-07-22 15:35 Fail-over PG server in connection string - syntax ? Mistina Michal <Michal.Mistina@virte.sk>
2013-07-23 06:23 ` Chen Huajun <chenhj@cn.fujitsu.com>

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