Skip to content

Transaction Control

Auto-Commit Mechanism

Since v2.3.0, a "server-side auto-commit + transaction-state fallback" mechanism is used: the connection pool is fixed at autocommit=True (built-in library behavior, no configuration parameter), and normal statements are auto-committed by the server. The client no longer parses SQL text to distinguish reads from writes; it only issues a fallback commit when the server reports that a transaction is still open.

Commit on Success

After a statement executes successfully, the client checks the server transaction state (the IN_TRANS flag):

  • No open transaction (regular reads/writes under autocommit=True): nothing is done, the server has already auto-committed;
  • An open transaction exists (e.g. init_sql set autocommit to 0): COMMIT is executed automatically.
# Write operations are auto-committed by the server, no manual control needed
async with engine.execute(
    "INSERT INTO users (name) VALUES (%s)", ("张三",)
) as result:
    pass  # Persisted on success

This mechanism treats all statements equally: WITH ... UPDATE/DELETE, writes starting with comments, OPTIMIZE, DO, and other statements missed by the old keyword whitelist are now committed correctly.

Rollback on Failure

When a statement fails, if the connection is in a transaction or autocommit is disabled, ROLLBACK is executed automatically; partial writes of a failed statement are never committed. Errors are still written to result.error without raising exceptions.

async with engine.execute("INSERT INTO users (id) VALUES (1)") as result:
    if result.error is not None:
        print(result.error_no, result.error_msg)  # Failure rolled back, just check the error

Commit Timing in Stream Mode

  • stream=True + no result set (INSERT/UPDATE/DELETE, etc.): committed immediately after execution according to the transaction state; writes are persisted normally;
  • stream=True + result set (SELECT, CALL returning result sets, etc.): an immediate COMMIT would break the streaming protocol, so the commit is deferred until the cursor is closed (close() / async with exit / data exhausted).
# Streaming reads: the transaction (if any) is committed when the cursor closes after reading
async with engine.execute(
    "SELECT * FROM large_table",
    stream=True
) as result:
    async for row in result:
        process(row)

No Cross-Statement Transactions

v2 does not support multi-statement transactions across execute() calls: a manual BEGIN / START TRANSACTION is committed immediately after that statement executes. For business logic requiring transaction boundaries, combine operations into a single statement or a stored procedure.

Connection Pool Configuration

The connection pool is created with autocommit=True as built-in library behavior:

  • Each connection defaults to auto-commit, consistent with the MySQL server default;
  • The client only issues a fallback COMMIT when the server reports an open transaction, and ROLLBACK on failure;
  • No autocommit configuration parameter is provided; if a backend rejects the SET AUTOCOMMIT handshake, the library internally falls back to the server default and emits a warning;
  • Non-transactional backends (such as ClickHouse) never set the transaction-state flag, so the client sends no redundant COMMIT.