如何在 JDBC 连接 URL 中为 SQL 服务器中的 failoverPartner 服务器指定端口

How to specify port in JDBC connection URL for failoverPartner server in SQL Server

我正在尝试添加一个服务器作为 SQL 服务器的故障转移,但它没有使用端口 1443,我正在使用端口 2776。我正在尝试指定它,但我尝试的一切都没有用。我该怎么做?

private String url = "jdbc:sqlserver://server1:2776;DatabaseName=db;failoverPartner=server2";

我已经尝试了以下配置,但其中 none 有效。

  1. ...failoverPartner=server2:2776
  2. ...failoverPartner=server2,2776
  3. ...failoverPartner=server2\db

但每次我都会遇到异常。

  1. com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host server2, port 1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

  2. com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host server2:2776, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

  3. com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host server2, 2776, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

来自文档 Connecting to SQL Server with the JDBC Driver - Setting the Connection Properties 文档,属性 failoverPartner:

Note: The driver does not support specifying the server instance port number for the failover partner instance as part of the failoverPartner property in the connection string. However, specifying the serverName, instanceName and portNumber properties of the principal server instance and failoverPartner property of the failover partner instance in the same connection string is supported.

措辞非常混乱,但您似乎无法为故障转移服务器指定端口号。


来自 Microsoft 的 KB-2284190 (Applications cannot connect to a mirror partner server when using port number in the failoverPartner attribute of the connection string ) 我读的几乎相同:

CAUSE

Both the error messages occur due to the fact that SQL Server JDBC drivers (all versions) do not support parsing of port number for FailoverPartner connection string attribute and rely on DNS and SQL Server Browser service (for named instances only) to resolve the connection information for the partner server. In environments wherein the conditions discussed in the Symptoms sections are met, the JDBC driver is not able to resolve the partner server information and hence you get the error message discussed above.

This behavior of SQL mirroring infrastructure is by design. For this reason, Microsoft JDBC Driver version 3.0 supports failover partner attribute value only in the format <server_name>[\<SQL_Server_instance_name>].

RESOLUTION

To work around this problem, use one of the following methods in environments where database mirroring is involved:

  • For default instances of SQL server that are part of mirroring configuration ensure that they are listening on the default port 1433 for TCP connections.

  • For named instances, ensure that SQL browser service is running and port 1434 is not blocked on the network and server B is not configured as a hidden instance.


最后,执行以下操作之一:

  • 指定故障转移服务器在端口 1433 上侦听 TCP。
  • 创建一个命名实例并将failoverPartner指定为server_name\instance_name。确保 SQL 服务器浏览器服务是 运行 并且端口 1434 未被阻止。