feat(sqlserver): support manual transactions with SQL Server 2000 compatibility

Closes #7542
This commit is contained in:
Rendegou authored and GitHub committed 2026-10-06 01:32:53 +08:00
1 parent 220dfa2170
commit 1539b6233c
61 files changed
+4861 -113

No files matched your search

-2
View File
@@ -5886,8 +5886,6 @@ dependencies = [
[[package]]
name = "tiberius"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1446cb4198848d1562301a3340424b4f425ef79f35ef9ee034769a9dd92c10d"
dependencies = [
"async-trait",
"asynchronous-codec",
+1
View File
@@ -42,6 +42,7 @@ tokio-postgres = { git = "https://github.com/t8y2/tokio-postgres-gaussdb.git", r
postgres-types = { git = "https://github.com/t8y2/tokio-postgres-gaussdb.git", rev = "ac8ad476de59093d33ab9f294531c956e0c6a724" }
postgres-protocol = { git = "https://github.com/t8y2/tokio-postgres-gaussdb.git", rev = "ac8ad476de59093d33ab9f294531c956e0c6a724" }
mysql_async = { git = "https://github.com/t8y2/mysql_async.git", rev = "b615bb0b10b5975182a4aa069cabc3d09021f6ab" }
tiberius = { path = "../../../vendor/tiberius" }
[profile.release]
panic = "abort"
@@ -1,6 +1,9 @@
package com.dbx.agent.sqlserverlegacy;
import com.dbx.agent.ConfiguredJdbcAgent;
import com.dbx.agent.ExecuteQueryOptions;
import com.dbx.agent.JdbcExecutor;
import com.dbx.agent.QueryResult;
import com.dbx.agent.ColumnInfo;
import com.dbx.agent.ConnectParams;
import com.dbx.agent.DdlBuilder;
@@ -63,6 +66,8 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
Arrays.asList("TABLE", "VIEW", "SYSTEM TABLE")
);
private volatile boolean sqlServer2000Mode;
private volatile boolean manualTransactionSession;
private volatile boolean manualTransactionExecuted;
public SqlServerLegacyAgent() {
super(PROFILE);
@@ -71,6 +76,114 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
enableLegacyTlsAlgorithms();
}
@Override
public Map<String, Object> beginManualTransaction(String schema) {
Connection conn = getConnection();
if (conn == null) throw new IllegalStateException("Not connected");
// Metadata is advisory when an old JDBC driver cannot implement it.
// A definitive false is different from an unavailable metadata method.
return unchecked(() -> {
java.sql.DatabaseMetaData metadata = null;
boolean unsupported = false;
try {
metadata = conn.getMetaData();
unsupported = !metadata.supportsTransactions();
} catch (SQLException | AbstractMethodError unavailable) {
// setAutoCommit is the authoritative operation in this case.
}
if (unsupported) throw new IllegalStateException("DBX_MANUAL_TRANSACTION_UNSUPPORTED: JDBC driver reports transactions are unsupported");
if (!conn.getAutoCommit()) throw new IllegalStateException("Manual transaction already open");
conn.setAutoCommit(false);
manualTransactionSession = true;
manualTransactionExecuted = false;
Map<String, Object> result = new LinkedHashMap<>();
result.put("ok", true);
result.put("manualTransactionBatch", true);
if (metadata != null) {
try {
result.put("productVersion", metadata.getDatabaseProductVersion());
result.put("productType", metadata.getDatabaseProductName());
result.put("driverName", metadata.getDriverName());
result.put("driverVersion", metadata.getDriverVersion());
} catch (SQLException | AbstractMethodError unavailable) {
// Diagnostics must not turn a successful BEGIN into a failure.
}
}
return result;
});
}
@Override
public Map<String, Object> commitManualTransaction() {
Connection conn = getConnection();
if (conn == null) throw new IllegalStateException("Not connected");
return unchecked(() -> {
if (conn.getAutoCommit()) throw new IllegalStateException("No manual transaction open");
if (manualTransactionExecuted && transactionCount(conn, 30) != 1) throw new IllegalStateException("DBX_MANUAL_TRANSACTION_STATE_LOST: commit state cannot be confirmed");
conn.commit();
// This is a dedicated, terminal session. Do not setAutoCommit(true):
// a failure in that cleanup must not obscure an acknowledged COMMIT.
return Collections.singletonMap("ok", (Object) true);
});
}
@Override
public Map<String, Object> rollbackManualTransaction() {
Connection conn = getConnection();
if (conn == null) throw new IllegalStateException("Not connected");
return unchecked(() -> {
if (conn.getAutoCommit()) throw new IllegalStateException("No manual transaction open");
if (manualTransactionExecuted && transactionCount(conn, 30) == 0) throw new IllegalStateException("DBX_MANUAL_TRANSACTION_STATE_LOST: transaction already ended; rollback cannot be confirmed");
conn.rollback();
return Collections.singletonMap("ok", (Object) true);
});
}
@Override
public boolean permitsAutomaticReconnect() {
return !manualTransactionSession;
}
@Override
public List<QueryResult> executeQueryResults(String sql, String schema, ExecuteQueryOptions options) {
Connection conn = getConnection();
if (!manualTransactionSession || conn == null) throw new IllegalStateException("No dedicated manual transaction connection");
return unchecked(() -> {
if (conn.getAutoCommit()) throw new IllegalStateException("DBX_MANUAL_TRANSACTION_STATE_LOST: autoCommit changed");
beforeQueryExecution(conn, options.getTimeoutSecs());
int before = transactionCount(conn, options.getTimeoutSecs());
if (before == 0 && !manualTransactionExecuted) {
// JDBC may defer its physical BEGIN. Open the DBX transaction before
// the first user batch, including PRINT/read-only procedures. Explicit
// BEGIN with IMPLICIT_TRANSACTIONS ON would open two transactions.
JdbcExecutor.current().executeAll(conn, "SET IMPLICIT_TRANSACTIONS OFF; BEGIN TRANSACTION", 1, null,
options.getTimeoutSecs(), resultValueReader());
before = transactionCount(conn, options.getTimeoutSecs());
}
if (before != 1) throw new IllegalStateException("DBX_MANUAL_TRANSACTION_STATE_LOST: transaction not active or nested");
manualTransactionExecuted = true;
List<QueryResult> results = JdbcExecutor.current().executeAll(conn, sql, options.getMaxRows(),
options.getFetchSize(), options.getTimeoutSecs(), resultValueReader());
int after = transactionCount(conn, options.getTimeoutSecs());
if (after != 1) {
throw new IllegalStateException("DBX_MANUAL_TRANSACTION_STATE_LOST: transaction ended or changed inside the batch");
}
return results;
});
}
private static int transactionCount(Connection conn, int timeoutSecs) throws SQLException {
try (java.sql.Statement stmt = conn.createStatement()) {
stmt.setQueryTimeout(timeoutSecs > 0 ? timeoutSecs : 30);
try (ResultSet rows = stmt.executeQuery("SELECT @@TRANCOUNT")) {
if (!rows.next()) throw new SQLException("Missing SQL Server transaction status");
int count = rows.getInt(1);
while (rows.next()) { /* consume status response */ }
return count;
}
}
}
@Override
public boolean supportsConnectionPooling() {
// The mssql-jdbc -> jTDS fallback is session state on this Agent
@@ -116,7 +229,7 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
throw fallbackError;
}
}
throw withLegacyTlsDiagnostics(error);
throw withLegacyTlsDiagnostics(error, jdbcDriverVersion(), requestedTlsProtocol(params));
}
}
@@ -141,6 +254,8 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
@Override
protected void afterDisconnect() {
manualTransactionSession = false;
manualTransactionExecuted = false;
sqlServer2000Mode = false;
}
@@ -305,7 +420,7 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
if (urlParams == null || urlParams.trim().isEmpty()) {
return properties;
}
for (String pair : urlParams.trim().split("[&;]")) {
for (String pair : connectionPropertyParts(urlParams.trim(), true)) {
String value = pair.trim();
int separator = value.indexOf('=');
if (separator <= 0) {
@@ -636,10 +751,70 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
Map<String, String> properties = baseConnectionProperties(params);
properties.put("encrypt", "true");
properties.put("trustServerCertificate", "true");
properties.put("sslProtocol", "TLSv1");
properties.put("sslProtocol", requestedTlsProtocol(params));
return appendProperties(baseJdbcUrl(params), properties);
}
private static String requestedTlsProtocol(ConnectParams params) {
// Keep the legacy default for existing SQL Server 2000 connections, but
// allow an explicit modern protocol when this driver is used on 2019/2022.
String protocol = tlsProtocolProperty(params.getConnection_string(), "TLSv1", false);
protocol = tlsProtocolProperty(params.getUrl_params(), protocol, true);
switch (protocol.toUpperCase(Locale.ROOT)) {
case "TLS": return "TLS";
case "TLSV1": return "TLSv1";
case "TLSV1.1": return "TLSv1.1";
case "TLSV1.2": return "TLSv1.2";
case "TLSV1.3": return "TLSv1.3";
default: throw new IllegalArgumentException("Unsupported SQL Server sslProtocol; use TLS, TLSv1, TLSv1.1, TLSv1.2 or TLSv1.3");
}
}
private static String tlsProtocolProperty(String properties, String fallback, boolean urlParams) {
if (properties == null) return fallback;
String protocol = fallback;
for (String property : connectionPropertyParts(properties, urlParams)) {
String part = property.trim();
while (part.startsWith("?")) part = part.substring(1).trim();
int separator = part.indexOf('=');
if (separator > 0 && "sslProtocol".equalsIgnoreCase(part.substring(0, separator).trim())) {
String value = part.substring(separator + 1).trim();
protocol = value.startsWith("{") && value.endsWith("}")
? value.substring(1, value.length() - 1).replace("}}", "}") : value;
}
}
return protocol;
}
private static List<String> connectionPropertyParts(String value, boolean ampersandDelimiter) {
List<String> parts = new ArrayList<>();
int start = 0;
boolean propertyValue = false;
boolean valueStarted = false;
boolean escaped = false;
for (int i = 0; i < value.length(); i++) {
char ch = value.charAt(i);
if (escaped) {
if (ch == '}') {
if (i + 1 < value.length() && value.charAt(i + 1) == '}') i++;
else escaped = false;
}
continue;
}
if (ch == ';' || (ampersandDelimiter && ch == '&')) {
parts.add(value.substring(start, i));
start = i + 1;
propertyValue = false;
valueStarted = false;
} else if (!propertyValue && ch == '=') {
propertyValue = true;
} else if (propertyValue && !valueStarted && !Character.isWhitespace(ch)) {
valueStarted = true;
escaped = ch == '{';
}
}
parts.add(value.substring(start));
return parts;
}
static String relaxedDisabledAlgorithms(String current) {
if (current == null || current.trim().isEmpty()) {
return "";
@@ -659,15 +834,15 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
}
static String legacyTlsDiagnostics() {
return legacyTlsDiagnostics(jdbcDriverVersion());
return legacyTlsDiagnostics(jdbcDriverVersion(), "TLSv1");
}
private static String legacyTlsDiagnostics(String jdbcVersion) {
private static String legacyTlsDiagnostics(String jdbcVersion, String protocol) {
String disabledAlgorithms = Security.getProperty(TLS_DISABLED_ALGORITHMS_KEY);
return "DBX SQL Server legacy TLS diagnostics: java=" + System.getProperty("java.version", "unknown")
+ ", javaVendor=" + System.getProperty("java.vendor", "unknown")
+ ", jdbc=" + jdbcVersion
+ ", sslProtocol=TLSv1"
+ ", sslProtocol=" + protocol
+ ", tlsV1Disabled=" + isDisabled(disabledAlgorithms, "TLSV1")
+ ", tlsRsaDisabled=" + isDisabled(disabledAlgorithms, "TLS_RSA_*")
+ ", rsaPkcs1Sha1HandshakeDisabled="
@@ -681,9 +856,13 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
}
static SQLException withLegacyTlsDiagnostics(SQLException error, String jdbcVersion) {
return withLegacyTlsDiagnostics(error, jdbcVersion, "TLSv1");
}
static SQLException withLegacyTlsDiagnostics(SQLException error, String jdbcVersion, String protocol) {
String message = error.getMessage() == null ? error.toString() : error.getMessage();
return new SQLException(
message + "\n\n" + legacyTlsDiagnostics(jdbcVersion),
message + "\n\n" + legacyTlsDiagnostics(jdbcVersion, protocol),
error.getSQLState(),
error.getErrorCode(),
error
@@ -774,7 +953,7 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
private static String sanitizeSqlServerUrl(String value) {
String trimmed = trimSqlServerUrl(value);
String[] parts = trimmed.split(";");
String[] parts = connectionPropertyParts(trimmed, false).toArray(new String[0]);
if (parts.length <= 1) {
return trimmed;
}
@@ -804,7 +983,7 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
return properties;
}
for (String pair : urlParams.trim().split("[&;]")) {
for (String pair : connectionPropertyParts(urlParams.trim(), true)) {
String value = pair.trim();
while (value.startsWith("?") || value.startsWith("&") || value.startsWith(";")) {
value = value.substring(1).trim();
@@ -27,6 +27,125 @@ import java.util.Locale;
import java.util.Map;
class SqlServerLegacyAgentTest {
private static final class ManualFixture {
final SqlServerLegacyAgent agent = new SqlServerLegacyAgent();
final java.util.concurrent.atomic.AtomicInteger count = new java.util.concurrent.atomic.AtomicInteger();
final java.util.concurrent.atomic.AtomicInteger begins = new java.util.concurrent.atomic.AtomicInteger();
final java.util.concurrent.atomic.AtomicInteger rollbacks = new java.util.concurrent.atomic.AtomicInteger();
final java.util.concurrent.atomic.AtomicBoolean automatic = new java.util.concurrent.atomic.AtomicBoolean(true);
ManualFixture() {
DatabaseMetaData metadata = proxy(DatabaseMetaData.class, (method, args) ->
method.getName().equals("supportsTransactions") ? true : defaultValue(method.getReturnType()));
TestSupport.setPrivateConnection(agent, proxy(Connection.class, (method, args) -> {
switch (method.getName()) {
case "getMetaData": return metadata;
case "getAutoCommit": return automatic.get();
case "setAutoCommit": automatic.set((boolean) args[0]); return null;
case "rollback": rollbacks.incrementAndGet(); count.set(0); return null;
case "createStatement":
return proxy(java.sql.Statement.class, (stmtMethod, stmtArgs) -> {
switch (stmtMethod.getName()) {
case "executeQuery":
java.util.concurrent.atomic.AtomicBoolean first = new java.util.concurrent.atomic.AtomicBoolean(true);
return proxy(ResultSet.class, (rowMethod, rowArgs) -> {
if (rowMethod.getName().equals("next")) return first.getAndSet(false);
if (rowMethod.getName().equals("getInt")) return count.get();
return defaultValue(rowMethod.getReturnType());
});
case "execute":
String sql = (String) stmtArgs[0];
if (sql.contains("BEGIN TRANSACTION")) { begins.incrementAndGet(); count.set(1); }
if (sql.contains("HIDDEN_COMMIT_ERROR")) { count.set(0); throw new SQLException("failure after hidden commit"); }
return false;
case "getUpdateCount": return -1;
default: return defaultValue(stmtMethod.getReturnType());
}
});
default: return defaultValue(method.getReturnType());
}
}));
agent.beginManualTransaction(null);
}
}
@Test
void lazyJdbcBeginOpensOnePhysicalTransactionBeforePrintAndReadOnlyExec() {
ManualFixture fixture = new ManualFixture();
Assertions.assertEquals(0, fixture.count.get());
fixture.agent.executeQueryResults("PRINT 'hello'", null, new com.dbx.agent.ExecuteQueryOptions(1, null, 5));
fixture.agent.executeQueryResults("EXEC('SELECT 1')", null, new com.dbx.agent.ExecuteQueryOptions(1, null, 5));
Assertions.assertEquals(1, fixture.begins.get());
Assertions.assertEquals(1, fixture.count.get());
Assertions.assertFalse(fixture.agent.permitsAutomaticReconnect());
fixture.agent.rollbackManualTransaction();
Assertions.assertEquals(1, fixture.rollbacks.get());
}
@Test
void hiddenCommitFollowedByErrorCannotBeReportedAsSuccessfulRollback() {
ManualFixture fixture = new ManualFixture();
Assertions.assertThrows(RuntimeException.class, () -> fixture.agent.executeQueryResults(
"HIDDEN_COMMIT_ERROR", null, new com.dbx.agent.ExecuteQueryOptions(1, null, 5)));
Assertions.assertEquals(0, fixture.count.get());
Assertions.assertThrows(IllegalStateException.class, () -> fixture.agent.rollbackManualTransaction());
Assertions.assertEquals(0, fixture.rollbacks.get());
Assertions.assertFalse(fixture.agent.permitsAutomaticReconnect());
}
@Test
void manualSessionDisablesReconnectAndCommitAckDoesNotDependOnAutocommitCleanup() {
SqlServerLegacyAgent agent = new SqlServerLegacyAgent();
java.util.concurrent.atomic.AtomicBoolean automatic = new java.util.concurrent.atomic.AtomicBoolean(true);
java.util.concurrent.atomic.AtomicInteger commits = new java.util.concurrent.atomic.AtomicInteger();
DatabaseMetaData metadata = proxy(DatabaseMetaData.class, (method, args) -> {
if (method.getName().equals("supportsTransactions")) return true;
return defaultValue(method.getReturnType());
});
TestSupport.setPrivateConnection(agent, proxy(Connection.class, (method, args) -> {
switch (method.getName()) {
case "getMetaData": return metadata;
case "getAutoCommit": return automatic.get();
case "setAutoCommit":
if ((boolean) args[0]) throw new SQLException("cleanup failed");
automatic.set(false); return null;
case "commit": commits.incrementAndGet(); return null;
default: return defaultValue(method.getReturnType());
}
}));
Assertions.assertTrue(agent.permitsAutomaticReconnect());
Assertions.assertEquals(true, agent.beginManualTransaction(null).get("manualTransactionBatch"));
Assertions.assertFalse(agent.permitsAutomaticReconnect());
Assertions.assertEquals(true, agent.commitManualTransaction().get("ok"));
Assertions.assertEquals(1, commits.get());
Assertions.assertFalse(agent.permitsAutomaticReconnect());
agent.disconnect();
Assertions.assertTrue(agent.permitsAutomaticReconnect());
}
@Test
void metadataUnsupportedRejectsBeforeChangingAutocommitButUnavailableMetadataUsesActualBegin() {
SqlServerLegacyAgent unsupported = new SqlServerLegacyAgent();
DatabaseMetaData metadata = proxy(DatabaseMetaData.class, (method, args) -> defaultValue(method.getReturnType()));
TestSupport.setPrivateConnection(unsupported, proxy(Connection.class, (method, args) -> {
if (method.getName().equals("getMetaData")) return metadata;
if (method.getName().equals("setAutoCommit")) throw new AssertionError("Unsupported BEGIN must not change the connection");
return defaultValue(method.getReturnType());
}));
Assertions.assertThrows(IllegalStateException.class, () -> unsupported.beginManualTransaction(null));
SqlServerLegacyAgent unavailable = new SqlServerLegacyAgent();
java.util.concurrent.atomic.AtomicBoolean automatic = new java.util.concurrent.atomic.AtomicBoolean(true);
TestSupport.setPrivateConnection(unavailable, proxy(Connection.class, (method, args) -> {
switch (method.getName()) {
case "getMetaData": throw new SQLException("metadata unavailable");
case "getAutoCommit": return automatic.get();
case "setAutoCommit": automatic.set((boolean) args[0]); return null;
default: return defaultValue(method.getReturnType());
}
}));
Assertions.assertEquals(true, unavailable.beginManualTransaction(null).get("ok"));
Assertions.assertFalse(automatic.get());
}
@Test
void sqlServer2000PreloginFailuresTriggerTheOldDriverFallback() {
// Real mssql-jdbc prelogin rejection for SQL Server 2000.
@@ -368,7 +487,7 @@ class SqlServerLegacyAgentTest {
}
@Test
void legacyTlsUrlUsesSqlServerTlsV1Properties() {
void legacyTlsUrlHonorsExplicitProtocolAndRetainsEncryption() {
ConnectParams params = new ConnectParams(
"db.example.com",
14330,
@@ -381,7 +500,7 @@ class SqlServerLegacyAgentTest {
);
Assertions.assertEquals(
"jdbc:sqlserver://db.example.com:14330;databaseName=appdb;applicationName=dbx;encrypt=true;trustServerCertificate=true;sslProtocol=TLSv1",
"jdbc:sqlserver://db.example.com:14330;databaseName=appdb;applicationName=dbx;encrypt=true;trustServerCertificate=true;sslProtocol=TLSv1.2",
SqlServerLegacyAgent.legacyTlsUrl(params)
);
}
@@ -458,11 +577,47 @@ class SqlServerLegacyAgentTest {
);
Assertions.assertEquals(
"jdbc:sqlserver://db.example.com:1433;databaseName=custom;applicationName=dbx;encrypt=true;trustServerCertificate=true;sslProtocol=TLSv1",
"jdbc:sqlserver://db.example.com:1433;databaseName=custom;applicationName=dbx;encrypt=true;trustServerCertificate=true;sslProtocol=TLSv1.2",
SqlServerLegacyAgent.legacyTlsUrl(params)
);
}
@Test
void legacyTlsUrlParametersOverrideUrlProtocolCaseInsensitively() {
ConnectParams params = new ConnectParams("ignored", 0, "", "sa", "secret",
"SSLPROTOCOL=tlsv1.2", "jdbc:sqlserver://db.example.com:1433;sslProtocol=TLSv1", false);
Assertions.assertEquals("jdbc:sqlserver://db.example.com:1433;encrypt=true;trustServerCertificate=true;sslProtocol=TLSv1.2",
SqlServerLegacyAgent.legacyTlsUrl(params));
}
@Test
void legacyTlsUrlRejectsInvalidProtocolRatherThanSilentlyDowngrading() {
ConnectParams params = new ConnectParams("db.example.com", 1433, "", "sa", "secret",
"sslProtocol=not-a-protocol", "", false);
Assertions.assertThrows(IllegalArgumentException.class, () -> SqlServerLegacyAgent.legacyTlsUrl(params));
}
@Test
void legacyTlsUrlSupportsDriverTlsV13() {
ConnectParams params = new ConnectParams("db.example.com", 1433, "", "sa", "secret",
"sslProtocol=tlsv1.3", "", false);
Assertions.assertTrue(SqlServerLegacyAgent.legacyTlsUrl(params).endsWith(";sslProtocol=TLSv1.3"));
}
@Test
void legacyTlsUrlPreservesBracedValuesAndIgnoresEmbeddedProtocolProperties() {
ConnectParams params = new ConnectParams("ignored", 0, "", "sa", "secret", "",
"jdbc:sqlserver://db.example.com:1433;applicationName={app;sslProtocol=invalid};sslProtocol={TLSv1.2}", false);
Assertions.assertEquals("jdbc:sqlserver://db.example.com:1433;applicationName={app;sslProtocol=invalid};encrypt=true;trustServerCertificate=true;sslProtocol=TLSv1.2",
SqlServerLegacyAgent.legacyTlsUrl(params));
params.setConnection_string("jdbc:sqlserver://db.example.com:1433;applicationName={app}};sslProtocol=invalid}");
Assertions.assertTrue(SqlServerLegacyAgent.legacyTlsUrl(params).endsWith(";sslProtocol=TLSv1"));
}
@Test
void legacyTlsDiagnosticsReportsExplicitProtocol() {
SQLException error = SqlServerLegacyAgent.withLegacyTlsDiagnostics(new SQLException("connection failed"), "13.2", "TLSv1.2");
Assertions.assertTrue(error.getMessage().contains("sslProtocol=TLSv1.2"));
}
@Test
void relaxedDisabledAlgorithmsRemovesOnlyLegacyTlsEntries() {
String current =