fix(sqlserver): jtds prelogin fallback and legacy 2000 support

Closes #9333
This commit is contained in:
zipg authored and GitHub committed 2026-09-17 00:18:07 +08:00
1 parent d35d578b03
commit 1a7b44f8fc
12 files changed
+380 -64

No files matched your search

@@ -96,7 +96,7 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
return connection;
} catch (SQLException error) {
logConnectionEvent("mssql-jdbc failed", params, error);
if (isSqlServer2000Unsupported(error)) {
if (shouldFallbackToJtds(error)) {
logConnectionEvent("switching to jTDS 1.3.1 fallback", params, null);
try {
super.loadDriver(jtdsDriverParams());
@@ -203,7 +203,7 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
.replace('\n', ' ');
}
static boolean isSqlServer2000Unsupported(Throwable error) {
static boolean shouldFallbackToJtds(Throwable error) {
Throwable current = error;
while (current != null) {
String message = current.getMessage();
@@ -215,16 +215,29 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
// "sql server 8" substring match, so accept both shapes.
boolean version8Rejection = (normalized.contains("sql server 8") || normalized.contains("sql server version 8"))
&& (normalized.contains("not support") || normalized.contains("不支持"));
// Other driver wordings name the supported floor instead.
boolean floor2005Rejection = normalized.contains("sql server 2005 or later");
if (version8Rejection || floor2005Rejection) {
// Other driver wordings name the supported floor instead. The
// localized mssql-jdbc resource keeps "SQL Server 2005" in
// English while translating the "or later" suffix.
boolean floor2005Rejection = normalized.contains("sql server 2005 or later")
|| (normalized.contains("sql server 2005") && normalized.contains("更高版本"));
// Some SQL Server 2000 installations close the TDS 7.4
// prelogin socket before mssql-jdbc can report the server
// version. In legacy mode, retry that handshake once with
// jTDS, which speaks the older protocol.
boolean preloginRejection = normalized.contains("connection reset")
|| normalized.contains("connection was reset")
|| normalized.contains("forcibly closed")
|| normalized.contains("意外的登录前响应")
|| (normalized.contains("unexpected")
&& (normalized.contains("prelogin") || normalized.contains("pre-login")));
if (version8Rejection || floor2005Rejection || preloginRejection) {
return true;
}
}
if (current instanceof SQLException) {
SQLException next = ((SQLException) current).getNextException();
if (next != null && next != current.getCause()) {
if (isSqlServer2000Unsupported(next)) {
if (shouldFallbackToJtds(next)) {
return true;
}
}
@@ -11,6 +11,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.SocketException;
import java.security.Security;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
@@ -26,27 +27,35 @@ import java.util.Map;
class SqlServerLegacyAgentTest {
@Test
void onlySqlServer8UnsupportedErrorsTriggerTheOldDriverFallback() {
// Real mssql-jdbc prelogin rejection for SQL Server 2000
// (R_unsupportedServerVersion, English-only resources).
Assertions.assertTrue(SqlServerLegacyAgent.isSqlServer2000Unsupported(
void sqlServer2000PreloginFailuresTriggerTheOldDriverFallback() {
// Real mssql-jdbc prelogin rejection for SQL Server 2000.
Assertions.assertTrue(SqlServerLegacyAgent.shouldFallbackToJtds(
new SQLException("SQL Server version 8 is not supported by this driver.")
));
// Older driver wordings name the supported floor instead
// (mssql-jdbc R_notSQLServer family).
Assertions.assertTrue(SqlServerLegacyAgent.isSqlServer2000Unsupported(
Assertions.assertTrue(SqlServerLegacyAgent.shouldFallbackToJtds(
new SQLException("This version of the driver can be used only with SQL Server 2005 or later.")
));
Assertions.assertTrue(SqlServerLegacyAgent.isSqlServer2000Unsupported(
Assertions.assertTrue(SqlServerLegacyAgent.shouldFallbackToJtds(
new SQLException("该驱动程序只能与 SQL Server 2005 或更高版本一起使用。")
));
Assertions.assertTrue(SqlServerLegacyAgent.shouldFallbackToJtds(
new SQLException("该驱动程序不支持 SQL Server 8 版")
));
Assertions.assertTrue(SqlServerLegacyAgent.isSqlServer2000Unsupported(
Assertions.assertTrue(SqlServerLegacyAgent.shouldFallbackToJtds(
new SQLException("The driver does not support SQL Server 8")
));
Assertions.assertFalse(SqlServerLegacyAgent.isSqlServer2000Unsupported(
Assertions.assertTrue(SqlServerLegacyAgent.shouldFallbackToJtds(
new SQLException("Connection failed", new SocketException("Connection reset"))
));
SQLException chained = new SQLException("Connection failed");
chained.setNextException(new SQLException("驱动程序收到意外的登录前响应。"));
Assertions.assertTrue(SqlServerLegacyAgent.shouldFallbackToJtds(chained));
Assertions.assertFalse(SqlServerLegacyAgent.shouldFallbackToJtds(
new SQLException("TLS handshake failed")
));
Assertions.assertFalse(SqlServerLegacyAgent.isSqlServer2000Unsupported(
Assertions.assertFalse(SqlServerLegacyAgent.shouldFallbackToJtds(
new SQLException("Login failed for user 'sa'")
));
}