JDBC 使用动态 TCP 端口连接到 SQL 服务器
JDBC Connection to SQL server with Dynamic TCP Ports
我的 sql 服务器引擎正在使用动态 TCP 端口,例如 52358。如果我在我的 JDBC 数据库中指定端口号 52358,我可以毫无问题地执行 JDBC 查询url 连接字符串,即
jdbc:sqlserver://serverName:52358;databaseName=myDB
但是,由于这是动态端口,如果下次重新启动 sql 服务器后此端口 (52358) 不可用,sql 服务器可能会选择另一个端口。
那么在连接字符串中配置我的数据库 URL 的最佳方法是什么?
我试过的方法:
省略端口号,仅使用实例名称,即
jdbc:sqlserver://serverName;databaseName=myDB
这行不通。顺便说一句,sql 服务器浏览器服务已经启用。
找到解决方案(感谢 Gord Thompson)。数据库URL的complete form是:
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
其中 portNumber 或 instanceName 可用于连接到 serverName。然而,
If both a portNumber and instanceName are used, the portNumber will
take precedence and the instanceName will be ignored.
因此,对于动态端口的情况,我们应该只使用instanceName进行连接,并保留SQL浏览器服务运行(SQL服务器提供SQL Server Browser Service , 以监视端口,并将传入连接定向到该实例的当前端口)。因此,在我的例子中,正确的形式是:
jdbc:sqlserver://serverName\instanceName;databaseName=myDB
此外,我们应该记住
For optimal connection performance, you should set the portNumber when
you connect to a named instance. This will avoid a round trip to the
server to determine the port number
然后这将调用一个静态端口号。
如果你看到官方文档here,他们说端口是可选的(他们建议你放一个),但看到这个注释:
For optimal connection performance, you should set the portNumber when you connect to a named instance. This will avoid a round trip to the server to determine the port number. If both a portNumber and instanceName are used, the portNumber will take precedence and the instanceName will be ignored.
这里我可以理解,如果省略端口,驱动程序会进行一些软扫描来确定使用哪个端口,请尝试不放置端口。
但请记住,为了获得最佳性能,他们建议明确选择一个。
如果您真的不能指望端口号保持不变,那么您将不得不使用 SQL 服务器实例名称进行连接。
默认实例:
jdbc:sqlserver://servername\MSSQLSERVER;...
命名实例
jdbc:sqlserver://servername\instancename;...
当然,这意味着SQL浏览器服务必须在服务器上运行,这样实例名称才能解析为当前正在使用的实际端口号。
我的 sql 服务器引擎正在使用动态 TCP 端口,例如 52358。如果我在我的 JDBC 数据库中指定端口号 52358,我可以毫无问题地执行 JDBC 查询url 连接字符串,即
jdbc:sqlserver://serverName:52358;databaseName=myDB
但是,由于这是动态端口,如果下次重新启动 sql 服务器后此端口 (52358) 不可用,sql 服务器可能会选择另一个端口。
那么在连接字符串中配置我的数据库 URL 的最佳方法是什么?
我试过的方法:
省略端口号,仅使用实例名称,即
jdbc:sqlserver://serverName;databaseName=myDB
这行不通。顺便说一句,sql 服务器浏览器服务已经启用。
找到解决方案(感谢 Gord Thompson)。数据库URL的complete form是:
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
其中 portNumber 或 instanceName 可用于连接到 serverName。然而,
If both a portNumber and instanceName are used, the portNumber will take precedence and the instanceName will be ignored.
因此,对于动态端口的情况,我们应该只使用instanceName进行连接,并保留SQL浏览器服务运行(SQL服务器提供SQL Server Browser Service , 以监视端口,并将传入连接定向到该实例的当前端口)。因此,在我的例子中,正确的形式是:
jdbc:sqlserver://serverName\instanceName;databaseName=myDB
此外,我们应该记住
For optimal connection performance, you should set the portNumber when you connect to a named instance. This will avoid a round trip to the server to determine the port number
然后这将调用一个静态端口号。
如果你看到官方文档here,他们说端口是可选的(他们建议你放一个),但看到这个注释:
For optimal connection performance, you should set the portNumber when you connect to a named instance. This will avoid a round trip to the server to determine the port number. If both a portNumber and instanceName are used, the portNumber will take precedence and the instanceName will be ignored.
这里我可以理解,如果省略端口,驱动程序会进行一些软扫描来确定使用哪个端口,请尝试不放置端口。 但请记住,为了获得最佳性能,他们建议明确选择一个。
如果您真的不能指望端口号保持不变,那么您将不得不使用 SQL 服务器实例名称进行连接。
默认实例:
jdbc:sqlserver://servername\MSSQLSERVER;...
命名实例
jdbc:sqlserver://servername\instancename;...
当然,这意味着SQL浏览器服务必须在服务器上运行,这样实例名称才能解析为当前正在使用的实际端口号。