fix(query): fall back to autocommit for legacy oracle agents

This commit is contained in:
zipg authored and GitHub committed 2026-09-02 11:20:20 +08:00
1 parent 10ac216964
commit 716d2eafd2
3 files changed
+124 -56

No files matched your search

@@ -92,6 +92,13 @@ export function isManualTransactionSessionExpired(error: unknown): boolean {
return message?.startsWith("Transaction session not found or expired;") === true || message === "Transaction was auto-rolled back due to 5 minutes of inactivity"; return message?.startsWith("Transaction session not found or expired;") === true || message === "Transaction was auto-rolled back due to 5 minutes of inactivity";
} }
export function isUnsupportedManualTransactionMethod(error: unknown): boolean {
const message = error instanceof Error ? error.message : typeof error === "string" ? error : undefined;
if (!message) return false;
const normalized = message.toLowerCase();
return normalized.includes("begin_manual_transaction") && (normalized.includes("unknown method") || normalized.includes("method not found"));
}
function normalizeBackendErrorAtDepth(error: unknown, seen: WeakSet<object>, depth: number): BackendError | null { function normalizeBackendErrorAtDepth(error: unknown, seen: WeakSet<object>, depth: number): BackendError | null {
if (depth > MAX_ERROR_PARSE_DEPTH) return null; if (depth > MAX_ERROR_PARSE_DEPTH) return null;
@@ -172,6 +172,45 @@ describe("queryStore manual transaction expiry recovery", () => {
expect(tab.result?.execution_error).toBe(true); expect(tab.result?.execution_error).toBe(true);
}); });
it("falls back to auto-commit for a read query when an old Oracle Agent lacks manual transactions", async () => {
mocks.beginManualTransaction.mockRejectedValue(new Error("BEGIN manual transaction failed: Agent RPC error (-1): unknown method: begin_manual_transaction"));
mocks.executeMulti.mockResolvedValue(successfulSelect());
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("oracle-1", "ORCL", "Query", "query", "APP");
store.setAutoCommit(tabId, false);
await store.executeTabSql(tabId, "SELECT 1 FROM DUAL");
const tab = store.tabs.find((item) => item.id === tabId)!;
expect(tab.autoCommit).toBe(true);
expect(tab.txnSessionId).toBeUndefined();
expect(mocks.beginManualTransaction).toHaveBeenCalledOnce();
expect(mocks.executeInManualTransaction).not.toHaveBeenCalled();
expect(mocks.executeMulti).toHaveBeenCalledOnce();
expect(tab.result?.rows).toEqual([[1]]);
});
it("does not replay a write when an old Oracle Agent lacks manual transactions", async () => {
mocks.beginManualTransaction.mockRejectedValue(new Error("BEGIN manual transaction failed: Agent RPC error (-1): Method not found: begin_manual_transaction"));
mocks.executeMulti.mockResolvedValue(successfulUpdate());
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("oracle-1", "ORCL", "Query", "query", "APP");
store.setAutoCommit(tabId, false);
await store.executeTabSql(tabId, "UPDATE USERS SET ACTIVE = 1");
const tab = store.tabs.find((item) => item.id === tabId)!;
expect(tab.autoCommit).toBe(false);
expect(mocks.beginManualTransaction).toHaveBeenCalledOnce();
expect(mocks.executeInManualTransaction).not.toHaveBeenCalled();
expect(mocks.executeMulti).not.toHaveBeenCalled();
expect(tab.result?.execution_error).toBe(true);
});
it("keeps raw JDBC connections in manual mode when their effective dialect is unsupported", async () => { it("keeps raw JDBC connections in manual mode when their effective dialect is unsupported", async () => {
mocks.getConnectionConfig.mockReturnValue({ mocks.getConnectionConfig.mockReturnValue({
id: "jdbc-1", id: "jdbc-1",
+78 -56
View File
@@ -56,6 +56,7 @@ import { normalizeResultPageSize } from "@/lib/dataGrid/paginationPageSize";
import { agentProtocolQueryResultMaxRows, capQueryResultTotal, effectiveQueryResultMaxRows, limitQueryPagination, queryResultLimitReached } from "@/lib/dataGrid/queryResultRowLimit"; import { agentProtocolQueryResultMaxRows, capQueryResultTotal, effectiveQueryResultMaxRows, limitQueryPagination, queryResultLimitReached } from "@/lib/dataGrid/queryResultRowLimit";
import { elasticsearchRestRequestRanges, executableStatementRanges, splitSqlStatementRanges } from "@/lib/sql/sqlStatementRanges"; import { elasticsearchRestRequestRanges, executableStatementRanges, splitSqlStatementRanges } from "@/lib/sql/sqlStatementRanges";
import { replaceSqlServerLeadingUseQuery, sqlServerLeadingUseScript, sqlServerUseDatabaseFromStatement } from "@/lib/sql/sqlCompletionLookupTarget"; import { replaceSqlServerLeadingUseQuery, sqlServerLeadingUseScript, sqlServerUseDatabaseFromStatement } from "@/lib/sql/sqlCompletionLookupTarget";
import { classifySqlRisk } from "@/lib/sql/sqlRisk";
import { externalSqlFileDisplayTitles, normalizeExternalSqlPath } from "@/lib/sql/sqlFileOpen"; import { externalSqlFileDisplayTitles, normalizeExternalSqlPath } from "@/lib/sql/sqlFileOpen";
import { clearDataGridPendingSnapshot, clearDataGridPendingSnapshotsForTab } from "@/composables/useDataGridEditor"; import { clearDataGridPendingSnapshot, clearDataGridPendingSnapshotsForTab } from "@/composables/useDataGridEditor";
import { clearDataGridStructuredFilterStatesForTab } from "@/lib/dataGrid/dataGridFilterBuilderPersistence"; import { clearDataGridStructuredFilterStatesForTab } from "@/lib/dataGrid/dataGridFilterBuilderPersistence";
@@ -72,7 +73,7 @@ import { useSavedSqlStore } from "@/stores/savedSqlStore";
import { useExportTracker } from "@/composables/useExportTracker"; import { useExportTracker } from "@/composables/useExportTracker";
import { recordQueryCancellationLatency, resourceLifecycleDiagnostics } from "@/lib/diagnostics/resourceLifecycleDiagnostics"; import { recordQueryCancellationLatency, resourceLifecycleDiagnostics } from "@/lib/diagnostics/resourceLifecycleDiagnostics";
import { appendDebugLog } from "@/lib/backend/debugLog"; import { appendDebugLog } from "@/lib/backend/debugLog";
import { BackendErrorException, formatError, isManualTransactionSessionExpired, normalizeBackendError, type BackendError } from "@/lib/backend/errorUtils"; import { BackendErrorException, formatError, isManualTransactionSessionExpired, isUnsupportedManualTransactionMethod, normalizeBackendError, type BackendError } from "@/lib/backend/errorUtils";
import { createSavedSqlEditorPosition, initSavedSqlEditorPositions, restoreSavedSqlEditorPosition, saveSavedSqlEditorPosition } from "@/lib/app/savedSqlEditorPosition"; import { createSavedSqlEditorPosition, initSavedSqlEditorPositions, restoreSavedSqlEditorPosition, saveSavedSqlEditorPosition } from "@/lib/app/savedSqlEditorPosition";
import { ensureSqlExtension } from "@/lib/savedSql/savedSqlFileName"; import { ensureSqlExtension } from "@/lib/savedSql/savedSqlFileName";
import { resolveSavedSqlExecutionTarget, savedSqlExecutionTargetFromTab, type SavedSqlExecutionTarget, type SavedSqlOpenTargetMode } from "@/lib/savedSql/savedSqlExecutionTarget"; import { resolveSavedSqlExecutionTarget, savedSqlExecutionTargetFromTab, type SavedSqlExecutionTarget, type SavedSqlOpenTargetMode } from "@/lib/savedSql/savedSqlExecutionTarget";
@@ -5408,45 +5409,7 @@ export const useQueryStore = defineStore("query", () => {
return false; return false;
} }
let executionPromise: Promise<QueryResult[]>; const executeWithoutManualTransaction = (): Promise<QueryResult[]> => {
if (tab.autoCommit === false) {
if (!tab.txnSessionId) {
queryExecutionLog("info", "begin-manual-txn:start", { traceId, elapsed: elapsed() });
tab.txnSessionId = await api.beginManualTransaction(executionConnectionId, executionDatabase, executionSchema, executionCatalog);
queryExecutionLog("info", "begin-manual-txn:done", { traceId, txnSessionId: tab.txnSessionId, elapsed: elapsed() });
}
queryExecutionLog("info", "execute-in-txn:invoke", { traceId, txnSessionId: tab.txnSessionId, elapsed: elapsed() });
executionDispatched = true;
// Only an initial manual execution classifies the user SQL (Oracle-only).
// A later cursor-page fetch must neither set nor clear the sticky bit.
const isInitialOracleManualExecution = effectiveDbType === "oracle" && !options?.pagination?.sessionId;
const classificationSql = isInitialOracleManualExecution ? queryBaseSql : undefined;
let manualTransactionRecoveryAttempted = false;
executionPromise = (async () => {
const txnSessionId = tab.txnSessionId;
if (!txnSessionId) throw new Error("Manual transaction session was not initialized");
const executeInTransaction = (sessionId: string) =>
useAgentResultSession
? api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, agentProtocolQueryResultMaxRows(queryResultMaxRows), useOracleLobPreview, pageLimit, options?.pagination?.sessionId, classificationSql)
: api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, pageLimit ?? agentProtocolQueryResultMaxRows(queryResultMaxRows), useOracleLobPreview, undefined, undefined, classificationSql);
try {
return await executeInTransaction(txnSessionId);
} catch (error) {
if (options?.pagination?.sessionId || manualTransactionRecoveryAttempted || !isManualTransactionSessionExpired(error)) throw error;
manualTransactionRecoveryAttempted = true;
// The expired session was discarded by the backend; the replacement
// session starts fresh, so the old sticky state resets with it.
clearOracleTxnPossiblyDirty(tab);
tab.txnSessionId = undefined;
tab.txnAutoRolledBack = true;
queryExecutionLog("info", "manual-txn:expired-recover", { traceId, elapsed: elapsed() });
const refreshedSessionId = await api.beginManualTransaction(executionConnectionId, executionDatabase, executionSchema, executionCatalog);
tab.txnSessionId = refreshedSessionId;
queryExecutionLog("info", "manual-txn:restarted", { traceId, txnSessionId: refreshedSessionId, elapsed: elapsed() });
return executeInTransaction(refreshedSessionId);
}
})();
} else {
queryExecutionLog("info", "execute-multi:start", { traceId, elapsed: elapsed() }); queryExecutionLog("info", "execute-multi:start", { traceId, elapsed: elapsed() });
// Query and data tabs use a tab-scoped pool so repeated executions keep // Query and data tabs use a tab-scoped pool so repeated executions keep
// connection-local state and avoid MySQL pool resets on every refresh. // connection-local state and avoid MySQL pool resets on every refresh.
@@ -5487,22 +5450,81 @@ export const useQueryStore = defineStore("query", () => {
clientSession: Boolean(executionClientSessionId), clientSession: Boolean(executionClientSessionId),
}); });
executionDispatched = true; executionDispatched = true;
executionPromise = return tab.batchSqlExecution && tab.batchSqlExecution.total > 1
tab.batchSqlExecution && tab.batchSqlExecution.total > 1 ? api.executeMultiWithProgress(
? api.executeMultiWithProgress( executionConnectionId,
executionConnectionId, executionDatabase,
executionDatabase, sqlToExecute,
sqlToExecute, (progress) => {
(progress) => { const current = findExecutionTab(id);
const current = findExecutionTab(id); if (current?.executionId === executionId) {
if (current?.executionId === executionId) { applyBatchSqlProgress(current, progress, continueOnBatchError, batchResume?.startStatementIndex ?? 0);
applyBatchSqlProgress(current, progress, continueOnBatchError, batchResume?.startStatementIndex ?? 0); }
} },
}, executionSchema,
executionSchema, { ...executionOptions, executionId },
{ ...executionOptions, executionId }, )
) : api.executeMulti(executionConnectionId, executionDatabase, sqlToExecute, executionSchema, executionId, executionOptions);
: api.executeMulti(executionConnectionId, executionDatabase, sqlToExecute, executionSchema, executionId, executionOptions); };
let executionPromise: Promise<QueryResult[]>;
if (tab.autoCommit === false) {
let useLegacyReadFallback = false;
if (!tab.txnSessionId) {
queryExecutionLog("info", "begin-manual-txn:start", { traceId, elapsed: elapsed() });
try {
tab.txnSessionId = await api.beginManualTransaction(executionConnectionId, executionDatabase, executionSchema, executionCatalog);
queryExecutionLog("info", "begin-manual-txn:done", { traceId, txnSessionId: tab.txnSessionId, elapsed: elapsed() });
} catch (error) {
const risk = classifySqlRisk(sqlToExecute, { dialect: effectiveDbType }).risk;
if (!isUnsupportedManualTransactionMethod(error) || risk !== "read") throw error;
tab.autoCommit = true;
clearManualTransactionSession(tab);
useLegacyReadFallback = true;
queryExecutionLog("warn", "begin-manual-txn:legacy-read-fallback", {
traceId,
elapsed: elapsed(),
databaseType: effectiveDbType,
});
}
}
if (useLegacyReadFallback) {
executionPromise = executeWithoutManualTransaction();
} else {
queryExecutionLog("info", "execute-in-txn:invoke", { traceId, txnSessionId: tab.txnSessionId, elapsed: elapsed() });
executionDispatched = true;
// Only an initial manual execution classifies the user SQL (Oracle-only).
// A later cursor-page fetch must neither set nor clear the sticky bit.
const isInitialOracleManualExecution = effectiveDbType === "oracle" && !options?.pagination?.sessionId;
const classificationSql = isInitialOracleManualExecution ? queryBaseSql : undefined;
let manualTransactionRecoveryAttempted = false;
executionPromise = (async () => {
const txnSessionId = tab.txnSessionId;
if (!txnSessionId) throw new Error("Manual transaction session was not initialized");
const executeInTransaction = (sessionId: string) =>
useAgentResultSession
? api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, agentProtocolQueryResultMaxRows(queryResultMaxRows), useOracleLobPreview, pageLimit, options?.pagination?.sessionId, classificationSql)
: api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, pageLimit ?? agentProtocolQueryResultMaxRows(queryResultMaxRows), useOracleLobPreview, undefined, undefined, classificationSql);
try {
return await executeInTransaction(txnSessionId);
} catch (error) {
if (options?.pagination?.sessionId || manualTransactionRecoveryAttempted || !isManualTransactionSessionExpired(error)) throw error;
manualTransactionRecoveryAttempted = true;
// The expired session was discarded by the backend; the replacement
// session starts fresh, so the old sticky state resets with it.
clearOracleTxnPossiblyDirty(tab);
tab.txnSessionId = undefined;
tab.txnAutoRolledBack = true;
queryExecutionLog("info", "manual-txn:expired-recover", { traceId, elapsed: elapsed() });
const refreshedSessionId = await api.beginManualTransaction(executionConnectionId, executionDatabase, executionSchema, executionCatalog);
tab.txnSessionId = refreshedSessionId;
queryExecutionLog("info", "manual-txn:restarted", { traceId, txnSessionId: refreshedSessionId, elapsed: elapsed() });
return executeInTransaction(refreshedSessionId);
}
})();
}
} else {
executionPromise = executeWithoutManualTransaction();
} }
const annotatedResults = annotateQueryResultSources(markQueryResultsRowsRaw(await withFrontendQueryTimeout(executionPromise, frontendTimeoutSecs, t("editor.queryTimeoutError", { seconds: frontendTimeoutSecs }))), queryBaseSql, sourceLabelDatabase, effectiveDbType, options?.sourceOffset); const annotatedResults = annotateQueryResultSources(markQueryResultsRowsRaw(await withFrontendQueryTimeout(executionPromise, frontendTimeoutSecs, t("editor.queryTimeoutError", { seconds: frontendTimeoutSecs }))), queryBaseSql, sourceLabelDatabase, effectiveDbType, options?.sourceOffset);
const results = offsetBatchQueryResultIndexes(annotatedResults.results, batchResume?.startStatementIndex ?? 0); const results = offsetBatchQueryResultIndexes(annotatedResults.results, batchResume?.startStatementIndex ?? 0);