使用DB2onCloud(也称为dashDB for Transactions)HA计划

时间:2017-10-11 18:57:40

标签: ibm-cloud dashdb

目前IBM Bluemix中的DB2onCloud具有HA计划,为了使应用程序在计划维护期间无缝连接到数据库,或者如果存在故障转移,我们如何才能在应用程序端执行此操作?

1 个答案:

答案 0 :(得分:1)

为了更好的安全性,在连接到云数据库上的Db2时,始终建议使用SSL。例如,要通过JDBC连接,您的连接字符串将如下所示:

"jdbc:db2://<Db2OnCloudServer>:50001/BLUDB:sslConnection=true;user=bluadmin;password=<Password>;enableSeamlessFailover=true;"

您可以从&#34; Service Credentials&#34;下的Db2 on Cloud服务实例获取此字符串和凭据。如果您还没有在那里看到凭据,请点击&#34;新凭据&#34;他们会出现。 使用SSL的另一个好处是,如果存在故障转移并且您使用上述连接字符串,它将无缝重新连接,因为服务器和客户端交换备用服务器信息。

当您不使用SSL连接时,您需要为要重新连接的应用程序指定其他参数,因为从服务器发送的备用服务器信息仅用于SSL连接。为此,您可以使用如下连接字符串:

"jdbc:db2://<DB2OnCloudServer>:50000/BLUDB:user=bluadmin;password=<Password>;enableClientAffinitiesList=1;maxRetriesForClientReroute=10;retryIntervalForClientReroute=5;clientRerouteAlternateServerName=<Db2OnCloudServer>,<Db2OnCloudServer>;clientRerouteAlternatePortNumber=50000,50000;enableSeamlessFailover=true;"

请注意,您将在字符串中指定与备用服务器相同的服务器。这是因为当发生故障转移时,服务器的IP将移动,因此连接始终通过相同的IP完成。通过指定clientRerouteAlternateServerNameclientRerouteAlternatePortNumber,它将覆盖从服务器返回的值,这意味着它将连接到ssl端口。

上面将介绍与数据库的实际连接,但您的应用程序还需要具有适当的重试逻辑。这是一个粗略的代码示例,显示了用法:     import java.sql。*;

public class JDBCSampleEx {

    public static void main(String args[]) {

        String connectionURL = "jdbc:db2://169.48.134.122:50000/BLUDB:user=bluadmin;password=MmM5OWQ3ZWUyZmNm;enableClientAffinitiesList=1;maxRetriesForClientReroute=10;retryIntervalForClientReroute=5;clientRerouteAlternateServerName=169.48.134.122,169.48.134.122;clientRerouteAlternatePortNumber=50000,50000;enableSeamlessFailover=true;";
        Connection con = null;

        try {

            // Load the JCC Driver class (db2jcc4.jar).
            Class.forName("com.ibm.db2.jcc.DB2Driver");

            //Create the connection using the static getConnection method
            con = DriverManager.getConnection(connectionURL);

            Statement stmt = con.createStatement();
            ResultSet rs = null;
            con.setAutoCommit(false);
            try {
                rs = stmt.executeQuery("select FIRSTNME, SALARY from EMPLOYEE");
                // Print results
                while (rs.next()) {
                    System.out.println("Name= " + rs.getString("FIRSTNME") + " SALARY= " + rs.getString("SALARY"));
                }
                // do a random update
                String sql = "update  EMPLOYEE set FIRSTNME = '" + RandomAlphaNum.gen(10) + "'";
                stmt.executeUpdate(sql);
                con.commit();
            } catch (java.sql.SQLException e) {
                //Catch return code to do any retry
                if (e.getErrorCode() == -30108 || e.getErrorCode() == -4498 || e.getErrorCode() == -4499) {
                    // Add any logic to reply the current in flight transaction
                    // if necessary
                    System.out.println("Replay any transactions if necessary");
                } else {
                    throw e;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                 if (con != null) {
                     con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}