You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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), but aiomysql forces autocommit=False during connection initialization, the session enters a state where every SELECT query starts an implicit transaction.
Example:
# aiomysql connection setup with autocommit=False (default) conn=awaitaiomysql.connect(..., autocommit=False)
This executes SET autocommit=0 on the server, overriding its default configuration.
Transaction Leak:
After executing a query (e.g., SELECT), the transaction remains open because autocommit=False.
When releasing the connection back to the pool, aiomysql checks conn.server_status to 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).
Root Cause:
The autocommit parameter in aiomysql.connect() defaults to False, conflicting with the server's actual configuration.
The library does not respect the server's autocommit value by default, forcing unnecessary transactions.
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.
Related Code: aiomysql forcibly sets autocommit during 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 SELECT query and release the connection back to the pool.
Observe the transaction status in MySQL (SHOW PROCESSLIST or INFORMATION_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.
$ python -m pip show aiomysqlName: aiomysqlVersion: 0.2.0Summary: MySQL driver for asyncio.Home-page: https://github.com/aio-libs/aiomysqlAuthor: Nikolay NovikAuthor-email: [email protected]License: MIT
PyMySQL Version
$ python -m pip show PyMySQLName: PyMySQLVersion: 1.1.1Summary: Pure Python MySQL DriverHome-page: Author: Author-email: Inada Naoki <[email protected]>, Yutaka Matsubara <[email protected]>License: MIT License
SQLAlchemy Version
$ python -m pip show sqlalchemyName: SQLAlchemyVersion: 1.4.54Summary: Database Abstraction LibraryHome-page: https://www.sqlalchemy.orgAuthor: Mike BayerAuthor-email: [email protected]License: MIT
OS
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.1
Additional context
No response
Code of Conduct
I agree to follow the aio-libs Code of Conduct
The text was updated successfully, but these errors were encountered:
The issue is likely related to the validation of connections when the connection pool releases them, as the default setting for autocommit is false according to the PEP-0245
specification.
Describe the bug
Issue Description:
When initializing a connection in
aiomysql
, the default value ofautocommit
is explicitly set toFalse
, which overrides the server's default configuration (typicallyTrue
). 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), butaiomysql
forcesautocommit=False
during connection initialization, the session enters a state where everySELECT
query starts an implicit transaction.Example:
This executes
SET autocommit=0
on the server, overriding its default configuration.Transaction Leak:
SELECT
), the transaction remains open becauseautocommit=False
.aiomysql
checksconn.server_status
to 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.METADATA LOCK
).Root Cause:
autocommit
parameter inaiomysql.connect()
defaults toFalse
, conflicting with the server's actual configuration.autocommit
value by default, forcing unnecessary transactions.Proposed Fix:
Set the default value of
autocommit
toNone
inaiomysql.Connection
, which would:SET autocommit=...
unless explicitly specified by the user.autocommit=True
, no transaction is started for read-only queries.References:
aiomysql
forcibly setsautocommit
during initializationSuggested Code Change:
Modify the
autocommit
default inaiomysql.Connection.__init__
fromFalse
toNone
: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
autocommit
.SELECT
query and release the connection back to the pool.SHOW PROCESSLIST
orINFORMATION_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
Python Version
aiomysql Version
PyMySQL Version
SQLAlchemy Version
OS
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
Additional context
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: