Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java应用程序中使用jdbc字符串错误进行PostgreSQL连接故障切换_Java_Postgresql_Jdbc_Connection_Failover - Fatal编程技术网

Java应用程序中使用jdbc字符串错误进行PostgreSQL连接故障切换

Java应用程序中使用jdbc字符串错误进行PostgreSQL连接故障切换,java,postgresql,jdbc,connection,failover,Java,Postgresql,Jdbc,Connection,Failover,我有一个小的测试应用程序,它可以查询带有主服务器(10.200.0.50)和备用服务器(10.200.0.51)的Postgresql集群。当应用程序与主服务器的连接不可用时,我希望应用程序重新指向备用服务器 我从这里开始阅读文档的最后一部分: 到目前为止,我有: public static void main(String[] args) { Scanner s = new Scanner(System.in); Statement stmt = null; Con

我有一个小的测试应用程序,它可以查询带有主服务器(10.200.0.50)和备用服务器(10.200.0.51)的Postgresql集群。当应用程序与主服务器的连接不可用时,我希望应用程序重新指向备用服务器

我从这里开始阅读文档的最后一部分:

到目前为止,我有:

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    Statement stmt = null;
    Connection c = null;
    try {
        Class.forName("org.postgresql.Driver");
        c = DriverManager
        .getConnection("jdbc:postgresql://10.200.0.50,10.200.0.51:5432/replicationtest",
            "postgres", "password");            

        stmt = c.createStatement();

        ResultSet rs = stmt.executeQuery( "SELECT * FROM testtable;" );
        while( rs.next() ) {
            String  name = rs.getString("name");
            System.out.println(name);
        }
        rs.close();

        int choice = s.nextInt();

        ResultSet rs2 = stmt.executeQuery("SELECT * FROM testtable");
        while( rs2.next() ) {
            String  name = rs2.getString("name");
            System.out.println(name);
        }
        rs2.close();
        c.close();

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getClass().getName()+": "+e.getMessage());
        System.exit(0);
    }
    s.close();
}
场景A)主服务器10.200.0.50关闭,备用服务器 10.200.0.51上升

这非常有效,一开始有一点延迟,但是应用程序使用jdbc字符串中指定的第二个服务器

场景B)运行应用程序时,主服务器和备用服务器都已启动。但是在第一次查询之后,应用程序正在等待nextInt(),我阻止了与10.200.0.50的连接;在成功建立连接后模拟主故障

当我运行场景B,然后调用第二个查询时,我得到错误:

org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:317)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:432)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:358)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:305)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:291)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:269)
at org.postgresql.jdbc.PgStatement.executeQuery(PgStatement.java:236)
at com.rakuten.pgRecovery.Main.main(Main.java:59)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)

我明白了,我需要将我的查询包装在一个try-catch中,并在读取失败时捕获错误。之后,我需要打开一个新的连接,该连接将在传递的节点上进行循环,从而获得一个良好的连接,就像在场景a中一样。我需要将我的查询包装在一个try-catch中,并在读取失败时捕获错误。之后,我需要打开一个新的连接,它将在传递的节点上进行循环,从而获得一个良好的连接,如场景a中所示