-
Notifications
You must be signed in to change notification settings - Fork 266
Description
Describe the bug
Issue Description:
When initializing a connection in aiomysql, the default value of autocommit is explicitly set to False, which overrides the server's default configuration (typically True). This leads to unexpected behavior where transactions are implicitly started but never committed, causing long-running transactions and potential table-locking issues.
Problem Details:
-
Autocommit Mismatch:
-
If the MySQL server has
autocommit=True(default), butaiomysqlforcesautocommit=Falseduring connection initialization, the session enters a state where everySELECTquery starts an implicit transaction. -
Example:
# aiomysql connection setup with autocommit=False (default) conn = await aiomysql.connect(..., autocommit=False)
This executes
SET autocommit=0on the server, overriding its default configuration.
-
Transaction Leak:
- After executing a query (e.g.,
SELECT), the transaction remains open becauseautocommit=False. - When releasing the connection back to the pool,
aiomysqlchecksconn.server_statusto determine if a transaction is active. However, due to a bug in status tracking, it incorrectly assumes no transaction is active and returns the connection to the pool without committing or rolling back. - The open transaction persists in the connection pool, leading to table locks (e.g., schema changes blocked by
METADATA LOCK).
- After executing a query (e.g.,
-
Root Cause:
- The
autocommitparameter inaiomysql.connect()defaults toFalse, conflicting with the server's actual configuration. - The library does not respect the server's
autocommitvalue by default, forcing unnecessary transactions.
- The
-
Proposed Fix:
Set the default value of autocommit to None in aiomysql.Connection, which would:
- Respect Server Configuration: Do not send
SET autocommit=...unless explicitly specified by the user. - Avoid Implicit Transactions: If the server defaults to
autocommit=True, no transaction is started for read-only queries.
References:
- MySQL Behavior: [autocommit Documentation](https://dev.mysql.com/doc/refman/8.0/en/innodb-autocommit-commit-rollback.html)
- Related Code:
aiomysqlforcibly setsautocommitduring initialization
Suggested Code Change:
Modify the autocommit default in aiomysql.Connection.__init__ from False to None:
def __init__(..., autocommit=None, ...):
...This ensures the server's autocommit configuration is respected unless explicitly overridden by the user.
Let me know if you need further details or testing assistance! 🙌
To Reproduce
- mysql server default autocommit=True.
- Initialize a connection pool without parameter
autocommit. - Execute a
SELECTquery and release the connection back to the pool. - Observe the transaction status in MySQL (
SHOW PROCESSLISTorINFORMATION_SCHEMA.INNODB_TRX), which shows an open transaction even after the connection is "closed".
Expected behavior
Respect Server Configuration: Do not send SET autocommit=... unless explicitly specified by the user.
or commit transaction when release conn to pool
Logs/tracebacks
MySQL [email protected]:mydb> SELECT * FROM `performance_schema`.`metadata_locks` LIMIT 0,100 \G;
***************************[ 1. row ]***************************
OBJECT_TYPE | TABLE
OBJECT_SCHEMA | mydb
OBJECT_NAME | table_name
COLUMN_NAME | <null>
OBJECT_INSTANCE_BEGIN | 140514755781136
LOCK_TYPE | SHARED_READ
LOCK_DURATION | TRANSACTION
LOCK_STATUS | GRANTED
SOURCE | sql_parse.cc:6142
OWNER_THREAD_ID | 902740
OWNER_EVENT_ID | 3
auto start transaction for select.Python Version
$ python --version
Python 3.10.14aiomysql Version
$ python -m pip show aiomysql
Name: aiomysql
Version: 0.2.0
Summary: MySQL driver for asyncio.
Home-page: https://github.com/aio-libs/aiomysql
Author: Nikolay Novik
Author-email: [email protected]
License: MITPyMySQL Version
$ python -m pip show PyMySQL
Name: PyMySQL
Version: 1.1.1
Summary: Pure Python MySQL Driver
Home-page:
Author:
Author-email: Inada Naoki <[email protected]>, Yutaka Matsubara <[email protected]>
License: MIT LicenseSQLAlchemy Version
$ python -m pip show sqlalchemy
Name: SQLAlchemy
Version: 1.4.54
Summary: Database Abstraction Library
Home-page: https://www.sqlalchemy.org
Author: Mike Bayer
Author-email: [email protected]
License: MITOS
Linux diaochan.huoban.ai 5.15.0-125-generic #135-Ubuntu SMP Fri Sep 27 13:53:58 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
or
Darwin Mac.local 24.4.0 Darwin Kernel Version 24.4.0: Wed Mar 19 21:16:34 PDT 2025; root:xnu-11417.101.15~1/RELEASE_ARM64_T6000 arm64
Database type and version
SELECT VERSION();
8.0.40-0ubuntu0.22.04.1Additional context
No response
Code of Conduct
- I agree to follow the aio-libs Code of Conduct

