Skip to content
C

Autocommit


Autocommit

Definition

Autocommit is a connection/session mode in which EVERY individual SQL statement is automatically wrapped in its own implicit transaction and committed immediately after it runs — no explicit START TRANSACTION/COMMIT needed, but also no way to group multiple statements into one atomic unit unless autocommit is turned off (or explicitly overridden with START TRANSACTION).

How It Works

With autocommit ON (the default in most client tools/drivers):

sql
UPDATE accounts SET balance = balance - 200 WHERE id = 1; -- committed immediately UPDATE accounts SET balance = balance + 200 WHERE id = 2; -- committed immediately, separately

If a crash happens between these two statements, the debit is permanently committed with NO corresponding credit — exactly the partial-transfer problem Atomicity (22.5) exists to prevent. Issuing an explicit START TRANSACTION temporarily suspends autocommit for that transaction's duration, regardless of the session's default mode, restoring the all-or-nothing grouping.

Edge Cases and Pitfalls

  • Autocommit being ON by default is precisely WHY explicit transaction boundaries (22.2) matter — any sequence of related statements that must succeed or fail together needs an explicit START TRANSACTION wrapped around them, or autocommit will commit each one independently.
  • Some client libraries/ORMs turn autocommit OFF by default and require an explicit commit() call — behavior is driver/library-specific, not a universal SQL-language guarantee, so it's important to know your specific tool's default.
  • Turning autocommit off entirely (a session-level setting in some databases) means EVERY statement, even a single SELECT, runs inside an implicitly-open transaction that must eventually be explicitly committed or rolled back — forgetting this can leave long-running open transactions that hold resources.

Key Takeaways

  • Autocommit ON means each statement is its own automatically-committed transaction — no explicit COMMIT is needed, but also no implicit grouping across statements.
  • Explicit START TRANSACTION overrides autocommit for that transaction's duration, enabling multi-statement atomicity.
  • Autocommit defaults vary by client/driver/ORM — always confirm your specific environment's behavior rather than assuming.

Mock Test

  • Autocommit - Quick Test

    8 questions on Autocommit.

    8 questions · 8 min · Medium
    Start Mock Test