fix(mysql): optionally keep explicit transactions in autocommit tabs

This commit is contained in:
zipg authored and GitHub committed 2026-09-22 11:11:06 +08:00
1 parent 74f1704d09
commit dc6fee7e93
35 files changed
+1274 -47

No files matched your search

+60 -2
View File
@@ -904,6 +904,39 @@ function getI18nT() {
}
}
/** Mirrors the MySQL auto-commit settlement the backend reported for this
* execution onto the tab. Absent markers (non-MySQL connections, executions
* that never touched a tab connection) leave the previous state untouched.
*
* The two rollback notices are deliberately different: a `BEGIN` the user
* typed is reported on every execution (the tab just lost real work), while a
* session sitting on `SET autocommit = 0` has an implicit transaction rolled
* back after *every* execution — that one is raised once per connection and
* re-armed only after the connection stops reporting it, so dismissing it
* does not bring it back on the next keystroke batch. */
export function applyAutoCommitTransactionReport(tab: QueryTab, results: QueryResult[]) {
const openTransaction = results.find((result) => result.auto_commit_open_transaction !== undefined)?.auto_commit_open_transaction;
if (openTransaction !== undefined) tab.autoCommitOpenTransaction = openTransaction;
const explicitRolledBack = results.some((result) => result.auto_commit_explicit_transaction_rolled_back === true);
const sessionRolledBack = results.some((result) => result.auto_commit_session_autocommit_rolled_back === true);
if (explicitRolledBack) {
tab.autoCommitTxnRolledBack = true;
tab.autoCommitSessionTxnRolledBackNotified = false;
return;
}
if (sessionRolledBack) {
if (!tab.autoCommitSessionTxnRolledBackNotified) {
tab.autoCommitSessionTxnRolledBack = true;
tab.autoCommitSessionTxnRolledBackNotified = true;
}
return;
}
// This execution rolled nothing back: the auto-commit-off session may be gone
// (or the execution never used the tab connection), so re-arm the notice for
// the next time it happens.
tab.autoCommitSessionTxnRolledBackNotified = false;
}
export const useQueryStore = defineStore("query", () => {
const redisMonitors = new Map<string, () => void>();
const t = getI18nT();
@@ -4881,6 +4914,16 @@ export const useQueryStore = defineStore("query", () => {
if (tab.txnPossiblyDirty !== undefined) tab.txnPossiblyDirty = false;
}
/** Auto-commit tabs mirror the backend's report of an open explicit
* transaction. The flag is dropped whenever the tab stops pointing at the
* connection that reported it (target switch, tab close), so a stale badge
* can never outlive the session it describes. */
function clearAutoCommitOpenTransaction(tab: { autoCommitOpenTransaction?: boolean; autoCommitSessionTxnRolledBack?: boolean; autoCommitSessionTxnRolledBackNotified?: boolean }) {
if (tab.autoCommitOpenTransaction !== undefined) tab.autoCommitOpenTransaction = false;
tab.autoCommitSessionTxnRolledBack = undefined;
tab.autoCommitSessionTxnRolledBackNotified = undefined;
}
/** Centralized manual-session cleanup. Clears every field tied to a manual
* transaction session exactly when that session is conclusively ended or
* discarded. Callers must not assign these fields individually. */
@@ -4903,11 +4946,19 @@ export const useQueryStore = defineStore("query", () => {
}
clearTxnPossiblyDirty(tab);
tab.txnAutoRolledBack = false;
clearAutoCommitOpenTransaction(tab);
}
async function commitTransaction(id: string) {
const tab = tabs.value.find((t) => t.id === id);
if (!tab?.txnSessionId) return;
if (!tab) return;
if (!tab.txnSessionId) {
// Auto-commit tab (`Tx:A`) that keeps explicit user transactions: the
// transaction lives on the tab's own connection, so COMMIT is an ordinary
// statement on that connection.
if (tab.autoCommitOpenTransaction) await executeCurrentSql("COMMIT", { tabId: tab.id });
return;
}
try {
await api.commitManualTransaction(tab.txnSessionId);
} finally {
@@ -4917,7 +4968,11 @@ export const useQueryStore = defineStore("query", () => {
async function rollbackTransaction(id: string) {
const tab = tabs.value.find((t) => t.id === id);
if (!tab?.txnSessionId) return;
if (!tab) return;
if (!tab.txnSessionId) {
if (tab.autoCommitOpenTransaction) await executeCurrentSql("ROLLBACK", { tabId: tab.id });
return;
}
const sessionId = tab.txnSessionId;
// Remove the old session before the backend responds: a target switch may
// start a new transaction while this rollback is still in flight.
@@ -7343,6 +7398,8 @@ export const useQueryStore = defineStore("query", () => {
timeoutSecs: queryTimeoutSecs,
catalog: executionCatalog,
continueOnError: continueOnBatchError,
// MySQL-family connections only use this; other drivers ignore it.
...(settingsStore.editorSettings.keepExplicitTransactionInAutoCommit ? { preserveExplicitTransaction: true } : {}),
};
queryExecutionLog("info", "execute-multi:invoke", {
traceId,
@@ -7527,6 +7584,7 @@ export const useQueryStore = defineStore("query", () => {
console.warn("[DBX] Failed to resolve SAP HANA CURRENT_SCHEMA", error);
}
}
if (tab.autoCommit !== false) applyAutoCommitTransactionReport(tab, results);
const current = findExecutionTab(id);
if (current?.executionId === executionId && manualTransactionTargetEpoch(current) === executionTargetEpoch) {
if (captureResultRun && current.isCancelling && restorePendingResultRun(current, executionId)) return false;